IOS執行個體 CALayer層動畫點擊

來源:互聯網
上載者:User

IOS執行個體 CALayer動畫點擊是本文要介紹的內容,利用CALayer可以實現複雜的動畫效果,同時CALayer在運動過程中,需要點擊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動畫點擊的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.