用UIImageView 的animationImages来做动画,系统没提供具体的结束事件,发现在一些机型上动画设置的动画时间有延迟,即设置动画时间为3.0秒,结果在3.0s的时候动画没有结束,
[self performSelector:@selector(endAinimation) withObject:nil afterDelay:3.0f];
所以导致在3.0秒后调用endAinimation的时候 把动画结束了,而动画却没播放完。代码如下:
static NSInteger const levelupImageCount = 46;
//最后一张图片
self.lastImagePath = [self getBundleImagePathWith:level num:levelupImageCount];
[_levelupImageView setImage:[UIImage imageWithContentsOfFile:self.lastImagePath]];
NSMutableArray * imageArray = [NSMutableArray array];
for (NSInteger i = 0; i <= levelupImageCount; i++) {
NSString * imagePath = [self getBundleImagePathWith:level num:i];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[imageArray addObject:image];
}
//ImageView动画
_levelupImageView.animationDuration = 3.0f;
//图片播放次数,1
_levelupImageView.animationRepeatCount = 1;
//设置动画图片数组
_levelupImageView.animationImages = imageArray;
[_levelupImageView startAnimating];
[self performSelector:@selector(endAinimation) withObject:nil afterDelay:animiationTime];
/*************升级动画结束后 添加选择和缩放动画*************/
- (void)endAinimation
{
[_levelupImageView stopAnimating];
_levelupImageView.animationImages = nil;
_cancelBtn.hidden = NO;
_rotateImageView.hidden = NO;
_shareBtn.hidden = NO;
[self addRotationAnimation];
[self addZoomAnimation];
}
为了动画结束时的精准控制,可以用CAAnimation提供的animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;方法,可以精确知道动画结束事件,
static NSInteger const levelupImageCount = 46;
//最后一张图片
self.lastImagePath = [self getBundleImagePathWith:level num:levelupImageCount];
[_levelupImageView setImage:[UIImage imageWithContentsOfFile:self.lastImagePath]];
NSMutableArray * imageArray = [NSMutableArray array];
for (NSInteger i = 0; i <= levelupImageCount; i++) {
NSString * imagePath = [self getBundleImagePathWith:level num:i];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
CGImageRef cgimg = image.CGImage;
[imageArray addObject:(__bridge UIImage *)cgimg];
}
//创建CAKeyframeAnimation
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.duration = 3.0f;
animation.delegate = self;
animation.values = imageArray;
animation.repeatCount = 1;
[_levelupImageView.layer addAnimation:animation forKey:@"levelup_show"];
/*************升级动画结束后 添加选择和缩放动画*************/
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[_levelupImageView.layer removeAnimationForKey:@"levelup_show"];
_cancelBtn.hidden = NO;
_rotateImageView.hidden = NO;
_shareBtn.hidden = NO;
[self addRotationAnimation];
[self addZoomAnimation];
}
图片都是本地的图片 而且都是@2x的图片,用CAKeyframeAnimation加载动画的时候图片会变得特别大,若用这个方法可能图片做成@1x的大小正好能展示(由于项目用xib做的,imageView的大小就是屏幕的大小,所以设置imageView的contentMode为UIViewContentModeCenter,这样image就会一直居中,方便不同屏幕的适配)
鉴于图片的原因,优化UIImageView的animationImages结束以后endAinimation:
/*************升级动画结束后 添加选择和缩放动画*************/
- (void)endAinimation
{
//UIImageView的动画时间不准,如果动画还在进行就等0.2秒
if(_levelupImageView.animating){
[self performSelector:@selector(endAinimation) withObject:nil afterDelay:0.2];
}else{
[_levelupImageView stopAnimating];
_levelupImageView.animationImages = nil;
[_levelupImageView setImage:[UIImage imageWithContentsOfFile:self.lastImagePath]];
_cancelBtn.hidden = NO;
_rotateImageView.hidden = NO;
_shareBtn.hidden = NO;
[self addRotationAnimation];
[self addZoomAnimation];
}
}
UIImageView的动画时间不准,如果动画结束的时间到了,进入endAinimation,动画在进行就等0.2秒再进入endAinimation,一直等动画真正的结束。
网友评论