iOS 点击屏幕show一个animationImages实现的动画

效果图如下:


图像 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,初步思索一下,使用动画组应该也可以实现该需求。如果有想到更好的实现方法。希望能够学习交流!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 需求:实现label部分文字点击,如下图 最容易实现的是UITextView,UITextView有三种实现方法。...
    9a957efaf40a阅读 5,812评论 3 7
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,127评论 2 59
  • 正常情况下,我们点击tabbar都只有一个变色效果,但有时候,如果我们想给它添加一个点击动画,该如何做呢? 先上几...
    纠结的哈士奇阅读 3,743评论 2 13
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,770评论 0 5
  • 城空了,有树长出来 我的城死了 铸起它的人,杀死它的人 不愿因为这件事而骄傲 一座城的终结 永远因为终结这件事而显...
    于十六阅读 2,902评论 6 17