当前位置: 首页 > news >正文

iPhone开发教程之实现 CALayer 层动画点击的代码

 利用 CALayer 可以实现复杂的动画效果,同时 CALayer 在运动过程中,需要点击 CALayer,同时能够监控到点击的对象。下面是实现的效果和过程。

实现过程:

#import "AnimView.h"
  @implementation AnimView
  - (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
  [self setBackgroundColor:[UIColor clearColor]];
  UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchesPoint:)];
  [self addGestureRecognizer:tapGesture];
  [tapGesture release];
  }
  return self;
  }
  -(void) drawRect:(CGRect)rect
  {
  [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stratAnim:) userInfo:nil repeats: NO];
  }
  -(void)stratAnim:(id)sender
  {
  //添加层
  layer2 = [CALayer layer];
  [layer2 setBackgroundColor:[[UIColor redColor] CGColor]];
  layer2.bounds = CGRectMake(0, 0, 60,40);//层设置为图片大小
  layer2.position = CGPointMake(25,25);//层在view的位置
  [self.layer addSublayer:layer2];//将层加到当前View的默认layer下
  [self startFlyStarAnimation];
  }
  -(void) startFlyStarAnimation
  {
  //运动轨迹
  CGMutablePathRef thePath=CGPathCreateMutable();
  CGPathMoveToPoint(thePath,NULL,self.center.x,self.center.y);
  CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y-45);
  CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y+45);
  CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y);
  //添加动画
  CAKeyframeAnimation * animation;
  animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
  animation.path=thePath;
  animation.duration=3.0;
  animation.repeatCount=2;
  CFRelease(thePath);
  [animation setDelegate:self];
  //[self animationDidStop:animation finished:YES];
  [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
  [layer2 addAnimation:animation forKey:kCATransition];
  }
  //动画停止
  -(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  {
  NSLog(@">>>>动画停止了");
  }
  //touch事件
  -(void)touchesPoint:(UITapGestureRecognizer *)gestureRecognizer
  {
  CGPoint locationInView = [gestureRecognizer locationInView:self];
  //presentationLayer layer的动画层
  CALayer *layer1=[[layer2 presentationLayer] hitTest:locationInView];
  if (layer1!=nil) {
  NSLog(@"点击了运动的layer");
  }
  }
  - (void)dealloc {
  [super dealloc];
  }
  @end
  其中 presentationLayer 是 CALayer 动画的位置层。



相关文章:

  • 调用数据库 缓存解决
  • MSSQL扫盲系列(开篇)
  • 给非Administrators组的用户设置IIS管理权限
  • C#中删除控件的事件的方法
  • VSS管理
  • php时间戳与date格式转换
  • Linq to entity 笔记
  • ARP协议的安全问题和安全威胁
  • keepalived配置文件解析系列之(一)keepalived简介及配置文件介绍
  • 如何学习吉日嘎拉的走火入魔C#.NET通用权限管理系统组件源码?
  • Firefox 7正式发布
  • 高效的使用stl::map和std::set
  • 深入理解JavaScript系列(22):S.O.L.I.D五大原则之依赖倒置原则DIP
  • 申请Google Map API Key
  • 系统集成项目管理工程师
  • 【Leetcode】104. 二叉树的最大深度
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • Java,console输出实时的转向GUI textbox
  • Java的Interrupt与线程中断
  • KMP算法及优化
  • learning koa2.x
  • Linux链接文件
  • React 快速上手 - 07 前端路由 react-router
  • Redash本地开发环境搭建
  • Vue 2.3、2.4 知识点小结
  • vue总结
  • 解析 Webpack中import、require、按需加载的执行过程
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 前端之Sass/Scss实战笔记
  • 入手阿里云新服务器的部署NODE
  • 事件委托的小应用
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (C++)八皇后问题
  • (安卓)跳转应用市场APP详情页的方式
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (一)插入排序
  • (转)可以带来幸福的一本书
  • (转载)Linux 多线程条件变量同步
  • .bashrc在哪里,alias妙用
  • .bat批处理出现中文乱码的情况
  • .net core 控制台应用程序读取配置文件app.config
  • .Net Core 中间件验签
  • .NET使用存储过程实现对数据库的增删改查
  • [16/N]论得趣
  • [17]JAVAEE-HTTP协议
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大
  • [Android]RecyclerView添加HeaderView出现宽度问题
  • [C#][DevPress]事件委托的使用
  • [C\C++]读入优化【技巧】
  • [C++] 如何使用Visual Studio 2022 + QT6创建桌面应用
  • [C++]priority_queue的介绍及模拟实现