//
// ViewController.m
// 逐帧动画
//
// Created by dc008 on 15/12/22.
// Copyright © 2015年 崔晓宇. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *_muArray;//存放动画的所有图片
UIImageView *_imageView;//显示动画的区域
UIImage *_image;//图片
CALayer *_layer;
int current;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_muArray = [NSMutableArray array];
current = 0;
// _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 298, 215)];
// [self.view addSubview:_imageView];
//创建图层
_layer = [[CALayer alloc]init];
_layer.frame = CGRectMake(0, 100, 298, 215);
[self.view.layer addSublayer:_layer];
//加载所有图片
for (int i= 1; i <= 87; i++) {
_image = [UIImage imageNamed:[NSString stringWithFormat:@"dazhao_%d",i]];
[_muArray addObject:_image];
}
//定义时钟对象
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
//添加时钟对象到主运行循环
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
// //设置动画
// _imageView.animationImages = _muArray;//动画数组(包括所有动画图片)
// _imageView.animationDuration = 0.05 * [_muArray count ];//1秒10张
// _imageView.animationRepeatCount = HUGE_VALF;//无限次重复
// [_imageView startAnimating];//开始动画
}
- (void)step{
static int s = 0;
s++;
if (s % 1 == 0) {
UIImage *image = _muArray[current];
_layer.contents = (id)image.CGImage;//更新图片
current = (current + 1) % 87;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end