// 1.加载图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
NSString *imageName = [NSString stringWithFormat:@"qd_%d", i + 1];
UIImage *image = [UIImage imageNamed:imageName];//imageName文件名 imageNamed:The image object for the specified file
[images addObject:image];
}
_imageView = [UIImageView new];
_imageView.frame = CGRectMake(100, 100, 320*ScreenWidths/750, 320*ScreenWidths/750);
[self.view addSubview:_imageView];
// 2. 把图片数组赋值给imageview的animationImages属性
_imageView.animationImages = images;
// 3.设置播放次数
_imageView.animationRepeatCount = 0;
// 4.设置执行时间
// 默认:一秒30帧.
// 当设置成1.0
_imageView.animationDuration = 1.5;
[_imageView startAnimating];
// [self.imageView stopAnimating];
// 暂停动画
- (void)pauseAnimation
{
// 取出当前的时间点,就是暂停的时间点
CFTimeInterval pausetime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
// 设定时间偏移量,让动画定格在那个时间点
[self.layer setTimeOffset:pausetime];
// 将速度设为0
[self.layer setSpeed:0.0f];
}
// 恢复动画
- (void)resumeAnimation
{
// 获取暂停的时间
CFTimeInterval pausetime = self.layer.timeOffset;
// 计算出此次播放时间(从暂停到现在,相隔了多久时间)
CFTimeInterval starttime = CACurrentMediaTime() - pausetime;
// 将时间偏移量设为0(重新开始);
self.layer.timeOffset = 0.0;
// 设置开始时间(beginTime是相对于父级对象的开始时间,系统会根据时间间隔帮我们算出什么时候开始动画)
self.layer.beginTime = starttime;
// 将速度恢复,设为1
self.layer.speed = 1.0;
}