序列帧的使用(汤姆猫为例)

想要实现序列帧播放动画,即出现汤姆猫那种效果,点击某一个按钮出现一系列动画动作,首先你要了解UIImageView里面的animationImages属性。点击UIImageView进入头文件,可以看到这样的代码:

//这是一个数组,往这里数组里放一组图片在设置相关属性即可实现序列帧播放。

@property(nonatomic,copy) NSArray *animationImages;            // The array must contain UIImages. Setting hides the single image. default is nil

我们接着看其他属性设置:

// 设置播放次数

@property(nonatomic) NSInteger animationRepeatCount; // 0 means infinite (default is 0)


// 设置播放时间

@property(nonatomic) NSTimeInterval animationDuration;         // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)


//开始、暂停、正在播放。

- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;


好了,介绍完属性,我们开始正式代码展示:

首先导入图片,这里展示一部分:

序列帧的使用(汤姆猫为例)_第1张图片


.m代码展示

#import "ViewController.h"

@interface ViewController ()

- (IBAction)drink;

/** 这是一只显示图片的猫 */
@property (weak, nonatomic) IBOutlet UIImageView *tom;

@end

@implementation ViewController


- (IBAction)drink {
    [self runAnimationWithCount:81 name:@"drink"];
    
}

/** 播放动画 */
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
    if (self.tom.isAnimating) return;
    
    // 1.加载所有的动画图片
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i






你可能感兴趣的:(iOS知识大体系)