IOS instance CALayer layer animation click

Source: Internet
Author: User

IOSInstanceCALayerLayerAnimationClick here to introduce the content of this article.CALayerCan achieve complexAnimationEffect, at the same timeCALayerYou need to clickCALayerClick object. The following describes the implementation results and processes. :

Implementation process:

 
 
  1. # Import "AnimView. h"
  2. @ Implementation AnimView
  3.  
  4. -(Id) initWithFrame :( CGRect) frame {
  5. Self = [super initWithFrame: frame];
  6. If (self ){
  7. [Self setBackgroundColor: [UIColor clearColor];
  8. UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (touchesPoint :)];
  9. [Self addGestureRecognizer: tapGesture];
  10. [TapGesture release];
  11. }
  12. Return self;
  13. }
  14. -(Void) drawRect :( CGRect) rect
  15. {
  16. [NSTimer scheduledTimerWithTimeInterval: 5 target: self selector: @ selector (stratAnim :) userInfo: nil repeats: NO];
  17. }
  18. -(Void) stratAnim :( id) sender
  19. {
  20. // Add Layer
  21. Layer2 = [CALayer layer];
  22. [Layer2 setBackgroundColor: [[UIColor redColor] CGColor];
  23. Layer2.bounds = CGRectMake (0, 0, 60, 40); // set the layer to the image size.
  24. Layer2.position = CGPointMake (25, 25); // The Position of the layer in the view
  25. [Self. layer addSublayer: layer2]; // Add the layer to the default layer of the current View.
  26. [Self startFlyStarAnimation];
  27. }
  28. -(Void) startFlyStarAnimation
  29. {
  30. // Motion Track
  31. CGMutablePathRef thePath = CGPathCreateMutable ();
  32. CGPathMoveToPoint (thePath, NULL, self. center. x, self. center. y );
  33. CGPathAddLineToPoint (thePath, NULL, self. center. x, self. center. y-45 );
  34. CGPathAddLineToPoint (thePath, NULL, self. center. x, self. center. y + 45 );
  35. CGPathAddLineToPoint (thePath, NULL, self. center. x, self. center. y );
  36. // Add an animation
  37. CAKeyframeAnimation * animation;
  38. Animation = [CAKeyframeAnimation animationWithKeyPath: @ "position"];
  39. Animation. path = thePath;
  40. Animation. duration = 3.0;
  41. Animation. repeatCount = 2;
  42. CFRelease (thePath );
  43. [Animation setDelegate: self];
  44. // [Self animationDidStop: animation finished: YES];
  45. [Animation setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
  46. [Layer2 addAnimation: animation forKey: kCATransition];
  47. }
  48. // Stop the animation
  49. -(Void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag
  50. {
  51. NSLog (@ ">>>> animation stopped ");
  52. }
  53. // Touch event
  54. -(Void) touchesPoint :( UITapGestureRecognizer *) gestureRecognizer
  55. {
  56. CGPoint locationInView = [gestureRecognizer locationInView: self];
  57. // The animation layer of the presentationLayer
  58. CALayer * layer1 = [[layer2 presentationLayer] hitTest: locationInView];
  59. If (layer1! = Nil ){
  60. NSLog (@ "click the moving layer ");
  61. }
  62. }
  63. -(Void) dealloc {
  64. [Super dealloc];
  65. }
  66. @ End

The presentationLayer is the position layer of the CALayer animation.

Source code: http://easymorse-iphone.googlecode.com/svn/trunk/iphone.presentlayer/

Summary:IOSInstanceCALayerLayerAnimationThe content you clicked has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.