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:
- # 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
- {
- // Add Layer
- Layer2 = [CALayer layer];
- [Layer2 setBackgroundColor: [[UIColor redColor] CGColor];
- Layer2.bounds = CGRectMake (0, 0, 60, 40); // set the layer to the image size.
- Layer2.position = CGPointMake (25, 25); // The Position of the layer in the view
- [Self. layer addSublayer: layer2]; // Add the layer to the default layer of the current View.
- [Self startFlyStarAnimation];
- }
- -(Void) startFlyStarAnimation
- {
- // Motion Track
- 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 );
- // Add an animation
- 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];
- }
- // Stop the animation
- -(Void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag
- {
- NSLog (@ ">>>> animation stopped ");
- }
- // Touch event
- -(Void) touchesPoint :( UITapGestureRecognizer *) gestureRecognizer
- {
- CGPoint locationInView = [gestureRecognizer locationInView: self];
- // The animation layer of the presentationLayer
- CALayer * layer1 = [[layer2 presentationLayer] hitTest: locationInView];
- If (layer1! = Nil ){
- NSLog (@ "click the moving layer ");
- }
- }
- -(Void) dealloc {
- [Super dealloc];
- }
- @ 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!