效果图如下:
图像 2.gif
实现代码:
// 先创建一个手势
UITapGestureRecognizer *animationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(AnimationTapAction:)];
animationTap.delegate = self;
/*
默认为YES,这种情况下当手势识别器识别到touch之后,会发送touchesCancelled给hit-
testview以取消hit-test view对touch的响应,这个时候只有手势识别器响应touch。
当设置成NO时,手势识别器识别到touch之后不会发送touchesCancelled给hit-test,这个时候手势识别器和hit-test view均响应touch。
*/
animationTap.cancelsTouchesInView = false;
[self.view addGestureRecognizer:animationTap];
//点击屏幕执行的方法
- (void)AnimationTapAction:(UITapGestureRecognizer*)sender {
CGPoint center = [sender locationInView:sender.view];
// 防止暴力点击 导致self.animationImageView释放失败
if (!self.animationImageView) {
self.animationImageView = [[UIImageView alloc] initWithImage:[Utils findImageWith:@""]];
}
NSMutableArray * imageAnimations = [[NSMutableArray alloc] init];
for (int i = 1; i < 20; i++) {
UIImage *image = [Utils findImageWith:[NSString stringWithFormat:@"animations_image_%d",i]];
CGImageRef cgimg = image.CGImage;
[imageAnimations addObject:(__bridge UIImage *)cgimg];
}
self.animationImageView.frame = CGRectMake(0, 0, 95, 95);
self.animationImageView.center = center;
[self.view addSubview:self.animationImageView];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.duration = 0.6f;
animation.delegate = self;
animation.values = imageAnimations;
animation.repeatCount = 1;
[self.animationImageView.layer addAnimation:animation forKey:@"levelup_show"];
}
//CAAnimationDelegate 代理方法
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if (flag) {
[self.animationImageView.layer removeAnimationForKey:@"levelup_show"];
[self.animationImageView removeFromSuperview];
}
}
为了快速实现产品的需求而想出来的一个办法。从技术上说,总感觉有点low,初步思索一下,使用动画组应该也可以实现该需求。如果有想到更好的实现方法。希望能够学习交流!
网友评论