CABasicAnimation (basic animation) and ioscabasicanimation
CAT/CAT sharing, must be excellent
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents
1. Overview of CABasicAnimation
Subclass of CAPropertyAnimation
Property parsing:
FromValue: Initial Value of the corresponding attribute of keyPath
ToValue: end value of the corresponding attribute of keyPath
As the Animation continues, the value of the corresponding attribute of keyPath gradually changes from fromValue to toValue within the duration of duration.
If fillMode = kCAFillModeForwards and removedOnComletion = NO, the layers remain in the animation state after the animation is executed. However, in essence, the attribute values of layers are the initial values before the animation is executed and are not actually changed. For example, the initial position value of CALayer is (0, 0), the fromValue of CABasicAnimation is (10, 10), and the toValue is (100,100). Although the layer remains at (100,100) after the animation is executed, the position of the substantive layer is (0, 0)
CAPropertyAnimation
CAPropertyAnimation is a subclass of CAAnimation and an abstract class. To create an animation object, use its two subclasses: CABasicAnimation and CAKeyframeAnimation.
Property parsing:
KeyPath: specify a CALayer attribute named keyPath (NSString type), and modify the value of this attribute of CALayer to achieve the corresponding animation effect. For example, if you specify @ "position" as the keyPath, modify the value of the position attribute of CALayer to achieve the animation effect of translation.
Ii. Pan Animation
Effect:
Code:
-(Void) viewDidLoad {[super viewDidLoad]; // 1. create layer CALayer * myLayer = [CALayer layer]; myLayer. bounds = CGRectMake (0, 0,100,100); myLayer. anchorPoint = CGPointZero; myLayer. position = CGPointMake (100,100); myLayer. backgroundColor = [UIColor greenColor]. CGColor; // 2. add the custom Layer to the Controller's view layer [self. view. layer addSublayer: myLayer]; self. myLayer = myLayer;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// 1. create a core animation CABasicAnimation * anima = [CABasicAnimation animation]; // 1.1 tells the system what animation to execute. keyPath = @ "position"; // specifies the animation to which the layer starts. // anima. fromValue = [NSValue valueWithCGPoint: CGPointMake (0, 0)]; // to which (to the specified position) anima. toValue = [NSValue valueWithCGPoint: CGPointMake (200,300)]; // how much is added based on the current position // anima. byValue = [NSValue valueWithCGPoint: CGPointMake (0,300)]; // set the animation time to anima. duration = 3; // 1.2 the animation is not deleted after the animation is executed. removedOnCompletion = NO; // 1.3 sets the animation to save the latest animation status. fillMode = kCAFillModeForwards; // 2. add the core animation to Layer [self. myLayer addAnimation: anima forKey: nil];}
Iii. Scaling Animation
Effect:
Code:
-(Void) viewDidLoad {[super viewDidLoad]; // 1. create layer CALayer * myLayer = [CALayer layer]; myLayer. bounds = CGRectMake (0, 0,100,100); myLayer. anchorPoint = CGPointZero; myLayer. position = CGPointMake (100,100); myLayer. backgroundColor = [UIColor greenColor]. CGColor; // 2. add the custom Layer to the Controller's view layer [self. view. layer addSublayer: myLayer]; self. myLayer = myLayer;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// 1. create a core animation CABasicAnimation * anima = [CABasicAnimation animation]; // 1.1 set the animation type anima. keyPath = @ "bounds"; // 1.2 do not delete the animation after the animation is set. removedOnCompletion = NO; // 1.3 sets the animation to save the latest animation status. fillMode = kCAFillModeForwards; // 1.4 set the animation time to anima. duration = 1; // 1.5 modify animation anima. toValue = [NSValue valueWithCGRect: CGRectMake (0, 0,200,200)]; // 2. add the core animation to Layer [self. myLayer addAnimation: anima forKey: nil];}
Iv. Rotating Animation
Effect:
Code:
-(Void) viewDidLoad {[super viewDidLoad]; // 1. create layer CALayer * myLayer = [CALayer layer]; myLayer. bounds = CGRectMake (0, 0,100,100); myLayer. anchorPoint = CGPointZero; myLayer. position = CGPointMake (100,100); myLayer. backgroundColor = [UIColor greenColor]. CGColor; // 2. add the custom Layer to the Controller's view layer [self. view. layer addSublayer: myLayer]; self. myLayer = myLayer;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// 1. create a core animation CABasicAnimation * anima = [CABasicAnimation animation]; // 1.1 set the animation type anima. keyPath = @ "transform"; // 1.2 do not delete the animation after the animation is executed. removedOnCompletion = NO; // 1.3 sets the animation to save the latest animation status. fillMode = kCAFillModeForwards; // 1.4 set the animation time to anima. duration = 1; // 1.5 modify animation anima. toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation (M_PI_4, 0, 0, 1)]; // 2. add the core animation to Layer [self. myLayer addAnimation: anima forKey: nil];}
V. set using transform (KVC)
Scale 1.5 times in y direction
Effect:
Code:
-(Void) viewDidLoad {[super viewDidLoad]; // 1. create layer CALayer * myLayer = [CALayer layer]; myLayer. bounds = CGRectMake (0, 0,100,100); myLayer. anchorPoint = CGPointZero; myLayer. position = CGPointMake (100,100); myLayer. backgroundColor = [UIColor greenColor]. CGColor; // 2. add the custom Layer to the Controller's view layer [self. view. layer addSublayer: myLayer]; self. myLayer = myLayer;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// 1. create a core animation CABasicAnimation * anima = [CABasicAnimation animation]; // 1.1 set the animation type // anima. keyPath = @ "transform. translation. x "; anima. keyPath = @ "transform. scale. y "; // 1.2 after the animation is set, the animation anima is not deleted. removedOnCompletion = NO; // 1.3 sets the animation to save the latest animation status. fillMode = kCAFillModeForwards; // 1.4 set the animation time to anima. duration = 1; // zooming 1.5 times in y direction. toValue = @ (1.5); // 2. add the core animation to Layer [self. myLayer addAnimation: anima forKey: nil];}