版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.09.17 |
前言
app中好的炫的動畫可以讓用戶耳目一新,為產(chǎn)品增色不少瞒大,關于動畫的實現(xiàn)我們可以用基本動畫而晒、關鍵幀動畫、序列幀動畫以及基于CoreGraphic的動畫等等螃壤,接下來這幾篇我就介紹下我可以想到的幾種動畫繪制方法抗果。具體Demo示例已開源到Github —— 刀客傳奇,感興趣的可以看我寫的另外幾篇奸晴。
1. 實現(xiàn)動畫方式深度解析(一) —— 播放GIF動畫(一)
2. 實現(xiàn)動畫方式深度解析(二) —— 播放GIF動畫之框架FLAnimatedImage的使用(二)
序列幀動畫
序列幀動畫就是將動畫的幀序列一幀一幀的播放冤馏,從而實現(xiàn)了動畫。
功能實現(xiàn)
下面我會給出示例代碼寄啼。
#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
功能驗證
下面看一下功能效果逮光。
每一個動畫都是序列幀的動畫。
后記
未完墩划,待續(xù)~~~