一.可以直接通过UIImageView提供的方法animationDuration进行一组图片的动画,具体代码如下:
dispatch_async(dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT), ^{
for (int i = 0; i < 87; i++) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"Bundle"];
//拼接路径
filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"backImage-%d.png", i]];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
[self.dataSour addObject:image];
}
dispatch_async(dispatch_get_main_queue(), ^{
_backView.animationImages = self.dataSour;
_backView.animationDuration = self.dataSour.count / 24;
_backView.animationRepeatCount = 1;
[_backView startAnimating];
[self performSelector:@selector(animationOver) withObject:nil afterDelay:(_backView.animationDuration + 1.1)];
});
});
- (void) animationOver
{
TBLog(@"调用了");
[_backView stopAnimating];
_backView.animationImages = nil;
}
1.大家可以看一下,在这废话不多说,直接说一下我的思路,加载图片的时候直接使用的是imageWithContentsOfFile方法,这里好处就不多说,相信大家都懂;
2.在向数组里面加载图片的时候,我是开一条线程,然后再子线程中进行此操作,原因很简单,就是因为此操作是耗时操作,如果放在主线程中进行操作,那么就会卡住线程,这里面我说一下我的使用场景:我的这个动画是放到登录页面的,如果我点击退出登录时需要重新创建登录页,此时正在进行添加图片到数组的耗时操作卡住主线程,造成的现象就是,点击“退出登录”按钮要等待1-2s中才能有反应,这样的体验很差
3.这个方法的最大优点就是操作简单,缺点就是耗内存,同时不很很准确的去监控动画的结束时间,这样一来就没办法很准确的在动画执行完毕后进行相应的操作,所以果断放弃,我们可以使用方法二进行替换。
二.使用CAKeyframeAnimation进行该动画的实现
dispatch_async(dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT), ^{
for (int i = 0; i < 87; i++) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"bundle"];
//拼接路径
filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"loginbackground_%d.png", i]];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
CGImageRef cgimg = image.CGImage;
[weakObject.dataSour addObject:(__bridge UIImage *)cgimg];
}
dispatch_async(dispatch_get_main_queue(), ^{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.duration = weakObject.dataSour.count / 24.0;
animation.delegate = weakObject;
animation.values = weakObject.dataSour;
[weakObject.backView.layer addAnimation:animation forKey:nil];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"bundle"];
//拼接路径
filePath = [filePath stringByAppendingPathComponent:@"loginbackground_83.png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
weakObject.backView.image = image;
});
});
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
TBLog(@"animationDidStop");
[self.dataSour removeAllObjects];
[UIView animateWithDuration:1.0 animations:^{
self.bottomView.alpha = 1.0;
}];
}
方法二的最大的优势就是可以准确的监控动画执行结束,这种方法解决我现在最大的痛点。但是它也不是完美的,在消耗内存方面,没有什么改善,我找了很多资料,但是到目前为止没有解决这个方法,希望各位大神有什么好的建议都可以进行交流。。。。