實現動畫方式深度解析(三) —— 播放序列幀動畫(一)

版本記錄

版本號 時間
V1.0 2017.09.17

前言

app中好的炫的動畫可以讓用戶耳目一新,為產品增色不少,關于動畫的實現我們可以用基本動畫、關鍵幀動畫、序列幀動畫以及基于CoreGraphic的動畫等等,接下來這幾篇我就介紹下我可以想到的幾種動畫繪制方法。具體Demo示例已開源到Github —— 刀客傳奇,感興趣的可以看我寫的另外幾篇。
1. 實現動畫方式深度解析(一) —— 播放GIF動畫(一)
2. 實現動畫方式深度解析(二) —— 播放GIF動畫之框架FLAnimatedImage的使用(二)

序列幀動畫

序列幀動畫就是將動畫的幀序列一幀一幀的播放,從而實現了動畫。


功能實現

下面我會給出示例代碼。

#import "ViewController.h"

#define kJJTomVCColumnNumber   2

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSDictionary *animationDict;

@end

@implementation ViewController

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.animationDict = [NSDictionary dictionary];
    
    [self loadDict];
    
    [self setupUI];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.imageView = imageView;
    self.imageView.image = [UIImage imageNamed:@"tom"];
    [self.view addSubview:imageView];
    
    for (NSString *temp in self.animationDict) {
        [self addButtonsWithKey:temp];
    }
}

- (void)addButtonsWithKey:(NSString *)key
{
    static NSInteger i = 0;
    NSInteger btnW = 30 ,btnH = 30, btnX ,btnY, spaceX = 250, spaceY = 200;
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:key forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setImage:[UIImage imageNamed:key] forState:UIControlStateNormal];
    btnX = 30 + spaceX * (i % kJJTomVCColumnNumber);
    btnY = 50 + spaceY * (i / kJJTomVCColumnNumber);
    button.frame = CGRectMake(btnX, btnY, btnW, btnH);
    i++;
    
    [button addTarget:self action:@selector(playAnimationWithkey:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)loadDict
{
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"tom.plist" withExtension:nil]];
    self.animationDict = dict;
}

#pragma mark - Action && Notification

- (void)playAnimationWithkey:(UIButton *)button
{
    NSMutableArray *animationArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < [self.animationDict[button.titleLabel.text] integerValue]; i++) {
        
        NSString *imageName = [NSString stringWithFormat:@"%@_%02zd",button.titleLabel.text,i];
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [animationArr addObject:image];
    }
    
    self.imageView.animationImages = animationArr;
    self.imageView.animationRepeatCount = 1;
    self.imageView.animationDuration = 2;
    [self.imageView startAnimating];
}

@end

功能驗證

下面看一下功能效果。

每一個動畫都是序列幀的動畫。

后記

未完,待續~~~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容