1熄求、顯示動(dòng)畫類型gif的圖片
? ? #import <YYImage.h>
? ? #import <YYAnimatedImageView.h>
? ? YYImage *gifImage = [YYImage imageNamed:@"yygif.gif"];
? ? YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:gifImage];
//? ? imageView.contentMode = UIViewContentModeScaleAspectFit;
? ? imageView.frame = CGRectMake(50, 50, 50, 50);
? ? [self.view addSubview:imageView];
2、精靈圖動(dòng)畫
? ? UIImage *spriteImage = [UIImage imageNamed:@"sprite.png"];//獲取圖片
? ? NSMutableArray *contentRectsAryM = [[NSMutableArray alloc] init];//精靈圖每一小張的Rect(大小和位置)
? ? NSMutableArray *frameAnimationDuration = [[NSMutableArray alloc] init];//與rect數(shù)組對(duì)應(yīng)逗概,每一小張對(duì)應(yīng)的動(dòng)畫時(shí)間
? ? NSInteger rowNum = 6.f;//一行上有幾小張
? ? NSInteger colNum = 8.f;//一列上有幾小張
? ? for (NSInteger i=0; i<rowNum; i++
? ? ? ? for (NSInteger j=0; j<colNum; j++
? ? ? ? ? ? CGRect rect = CGRectMake(i*spriteImage.size.width/rowNum, j*spriteImage.size.height/colNum, spriteImage.size.width/rowNum, spriteImage.size.height/colNum);
? ? ? ? ? ? NSValue *rectValue = [NSValue valueWithCGRect:rect];
? ? ? ? ? ? [contentRectsAryM addObject:rectValue];
? ? ? ? ? ? [frameAnimationDuration addObject:@(1/15.f)];
? ? ? ? }
? ? }
? ? YYSpriteSheetImage *yyspriteImage = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:spriteImage contentRects:contentRectsAryM frameDurations:frameAnimationDuration loopCount:0];//UIImage轉(zhuǎn)為YYSpriteSheetImage
//? ? 創(chuàng)建視圖 加載YYSpriteSheetImage
? ? YYAnimatedImageView *spriteImageView = [[YYAnimatedImageView alloc] initWithImage:yyspriteImage];
? ? spriteImageView.frame = CGRectMake(50, 50+270*0.5+10, spriteImage.size.width/6.f, spriteImage.size.height/8.f);
? ? spriteImageView.clipsToBounds = YES;
? ? [self.view addSubview:spriteImageView];
3弟晚、幀動(dòng)畫,切換圖片實(shí)現(xiàn)動(dòng)畫效果:
坑點(diǎn):要獲取到圖片的路徑,所以不要將圖片放到類似images.xcasset里面卿城,這里面的圖片只能通過 ... imageName...獲取到枚钓,要將圖片直接放到工程目錄,然后通過
[[NSBundle mainBundle] pathForResource:xxx ofType:nil]來獲取圖片路徑
? ? NSMutableArray *imagePathsM = [NSMutableArray array];//存有圖片路徑的數(shù)組
? ? for (int p=0; p<8; p++) {
? ? ? ? NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d.png", p+1] ofType:nil];
? ? ? ? [imagePathsM addObject:path];
? ? }
? ? NSArray *times = @[@0.1, @0.2, @0.1, @0.1, @0.1, @0.1, @0.1, @0.1];//每張圖的動(dòng)畫時(shí)間
? ? YYFrameImage *frameImage = [[YYFrameImage alloc] initWithImagePaths:imagePathsM frameDurations:times loopCount:0];//0為無限循環(huán)
? ? YYAnimatedImageView *imageViewFrame = [[YYAnimatedImageView alloc] initWithImage:frameImage];
? ? imageViewFrame.frame = CGRectMake(50, 50, 210, 100);
? ? [self.view addSubview:imageViewFrame];
4瑟押、進(jìn)度控制:
YYImage//gif等其他
YYSpriteSheetImage//精靈圖
YYFrameImage//幀動(dòng)畫
共有屬性?animatedImageFrameCount 即圖片張數(shù)
animatedImageFrameCount*progress//progress視為進(jìn)度搀捷,再通過
YYAnimatedImageView的屬性currentAnimatedImageIndex來控制動(dòng)畫的進(jìn)度!
注意:在精靈圖動(dòng)畫,控制進(jìn)度有效的條件是動(dòng)畫必須在執(zhí)行多望,如果動(dòng)畫停止了嫩舟,進(jìn)度就無法調(diào)整了
? ? ? ? imageView.currentAnimatedImageIndex = gifImage.animatedImageFrameCount*0.2;
? ? ? ? imageViewFrame.currentAnimatedImageIndex = frameImage.animatedImageFrameCount*0.2;
? ? ? ? spriteImageView.currentAnimatedImageIndex = yyspriteImage.animatedImageFrameCount*0.5;
YYAnimatedImageView?autoPlayAnimatedImage屬性控制圖片動(dòng)畫是否自動(dòng)播放