IOS实例 CALayer层动画点击

移动开发 iOS
本文介绍的IOS实例 CALayer层动画点击,CALayer在运动过程中,需要点击CALayer,同时能够监控到点击的对象,来看具体内容。

IOS实例 CALayer动画点击是本文要介绍的内容,利用CALayer可以实现复杂的动画效果,同时CALayer在运动过程中,需要点击CALayer,同时能够监控到点击的对象。下面是实现的效果和过程。如图所示:

IOS实例 CALayer层动画点击

实现过程:

  1. #import "AnimView.h"   
  2. @implementation AnimView  
  3.  
  4. - (id)initWithFrame:(CGRect)frame {   
  5.       
  6.     self = [super initWithFrame:frame];   
  7.     if (self) {   
  8.         [self setBackgroundColor:[UIColor clearColor]];   
  9.         UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchesPoint:)];   
  10.         [self addGestureRecognizer:tapGesture];   
  11.         [tapGesture release];   
  12.     }   
  13.     return self;   
  14. }   
  15. -(void) drawRect:(CGRect)rect   
  16. {   
  17.     [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stratAnim:) userInfo:nil repeats: NO];   
  18. }   
  19. -(void)stratAnim:(id)sender   
  20. {   
  21.     //添加层   
  22.     layer2 = [CALayer layer];   
  23.     [layer2 setBackgroundColor:[[UIColor redColor] CGColor]];   
  24.     layer2.bounds = CGRectMake(0, 0, 60,40);//层设置为图片大小   
  25.     layer2.position = CGPointMake(25,25);//层在view的位置   
  26.     [self.layer addSublayer:layer2];//将层加到当前View的默认layer下   
  27.       
  28.     [self startFlyStarAnimation];   
  29. }   
  30. -(void) startFlyStarAnimation   
  31. {      
  32.     //运动轨迹   
  33.     CGMutablePathRef thePath=CGPathCreateMutable();   
  34.     CGPathMoveToPoint(thePath,NULL,self.center.x,self.center.y);   
  35.     CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y-45);   
  36.     CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y+45);   
  37.     CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y);   
  38.       
  39.     //添加动画   
  40.     CAKeyframeAnimation * animation;   
  41.     animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];   
  42.     animation.path=thePath;   
  43.     animation.duration=3.0;   
  44.     animation.repeatCount=2;   
  45.     CFRelease(thePath);   
  46.     [animation setDelegate:self];   
  47.     //[self animationDidStop:animation finished:YES];   
  48.     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];   
  49.     [layer2 addAnimation:animation forKey:kCATransition];   
  50. }   
  51. //动画停止   
  52. -(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag   
  53. {   
  54.     NSLog(@">>>>动画停止了");   
  55. }   
  56. //touch事件   
  57. -(void)touchesPoint:(UITapGestureRecognizer *)gestureRecognizer   
  58. {   
  59.     CGPoint locationInView = [gestureRecognizer locationInView:self];   
  60.     //presentationLayer layer的动画层   
  61.    CALayer *layer1=[[layer2 presentationLayer] hitTest:locationInView];   
  62.     if (layer1!=nil) {   
  63.         NSLog(@"点击了运动的layer");   
  64.     }   
  65. }   
  66. - (void)dealloc {   
  67.     [super dealloc];   
  68. }   
  69. @end  

其中presentationLayer是CALayer动画的位置层。

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.presentlayer/

小结:IOS实例 CALayer动画点击的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-22 18:20:04

IOS View 动画

2015-03-24 09:37:50

SwiftiOSCALayer

2012-12-24 13:38:01

iOSUIView

2015-06-18 10:33:02

iOS粘性动画

2011-07-03 10:05:52

Core Animat

2009-06-29 14:21:00

无线网络家庭组网

2011-05-11 10:28:03

2015-12-30 14:16:05

iOS动画视图渲染

2015-12-23 09:16:33

ios动画渲染机制

2013-07-29 05:01:31

iOS开发iOS开发学习按钮拖动和点击

2011-08-09 17:16:56

CoreAnimati动画

2011-07-26 15:56:53

iPhone 游戏 启动画面

2011-06-24 16:09:24

Qt 动画 状态机

2011-07-18 16:57:36

Core Animation 动画

2015-07-27 10:27:32

IOS基础知识核心动画

2011-07-22 16:47:53

iOS 通知 xcode

2011-07-26 11:08:23

iOS 录像 录音

2009-07-28 15:08:50

MVC三层架构实例

2010-09-14 14:42:24

FlashDIV CSS

2011-04-29 13:15:16

iOS程序图标iOS图标设计iOS
点赞
收藏

51CTO技术栈公众号