YYKit播放图片动画(gif/帧动画/精灵图动画)

1、显示动画类型gif的图片

    #import

    #import

    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、精灵图动画

    UIImage *spriteImage = [UIImage imageNamed:@"sprite.png"];//获取图片

    NSMutableArray *contentRectsAryM = [[NSMutableArray alloc] init];//精灵图每一小张的Rect(大小和位置)

    NSMutableArray *frameAnimationDuration = [[NSMutableArray alloc] init];//与rect数组对应,每一小张对应的动画时间

    NSInteger rowNum = 6.f;//一行上有几小张

    NSInteger colNum = 8.f;//一列上有几小张

    for (NSInteger i=0; i

        for (NSInteger j=0; 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转为YYSpriteSheetImage

//    创建视图 加载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、帧动画,切换图片实现动画效果:

坑点:要获取到图片的路径,所以不要将图片放到类似images.xcasset里面,这里面的图片只能通过 ... imageName...获取到,要将图片直接放到工程目录,然后通过

[[NSBundle mainBundle] pathForResource:xxx ofType:nil]来获取图片路径

    NSMutableArray *imagePathsM = [NSMutableArray array];//存有图片路径的数组

    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];//每张图的动画时间

    YYFrameImage *frameImage = [[YYFrameImage alloc] initWithImagePaths:imagePathsM frameDurations:times loopCount:0];//0为无限循环

    YYAnimatedImageView *imageViewFrame = [[YYAnimatedImageView alloc] initWithImage:frameImage];

    imageViewFrame.frame = CGRectMake(50, 50, 210, 100);

    [self.view addSubview:imageViewFrame];

4、进度控制:

YYImage//gif等其他

YYSpriteSheetImage//精灵图

YYFrameImage//帧动画

共有属性 animatedImageFrameCount 即图片张数

animatedImageFrameCount*progress//progress视为进度,再通过

YYAnimatedImageView的属性currentAnimatedImageIndex来控制动画的进度!

注意:在精灵图动画,控制进度有效的条件是动画必须在执行,如果动画停止了,进度就无法调整了

        imageView.currentAnimatedImageIndex = gifImage.animatedImageFrameCount*0.2;

        imageViewFrame.currentAnimatedImageIndex = frameImage.animatedImageFrameCount*0.2;

        spriteImageView.currentAnimatedImageIndex = yyspriteImage.animatedImageFrameCount*0.5;

YYAnimatedImageView autoPlayAnimatedImage属性控制图片动画是否自动播放

你可能感兴趣的:(YYKit播放图片动画(gif/帧动画/精灵图动画))