OC, oc Language
Overview
Introduction
- CAKeyframeAnimation is also called a key frame animation.
- CAKeyframeAnimation is a subclass of the abstract class CAPropertyAnimation. It can be directly used.
- Specify the animation attributes through the values and path attributes.
Notes
- If the path attribute is specified, the values attribute is ignored.
- CABasicAnimation is equivalent to CAKeyframeAnimation with only two key frames.
Common attributes of Key Frame Animation
Values (NSArray *)
- Multiple key frame values
- Similar to the fromValue and toValue values of CABasicAnimation
Path (CGPathRef)
- Animation execution path
- You can draw a path by drawing
KeyTimes (NSArray *)
- Execution time of each key frame
- NSNumber type
- If this parameter is not specified, the duration of all key frames is equal to the animation duration.
TimingFunctions (NSArray *)
- Array of speed control functions
CalculationMode (NSString *)
- Specifies the animation attribute of the key frame.
- If this value is specified, the keyTimes and timingFunctions attribute values are ignored.
- Default Value: kCAAnimationLinear
RotationMode (NSString *)
- Specify the rotation mode. The default value is nil.
Example
Implementation
- Draw the besell curve by listening to the touch events of the UI control that executes the animation.
- Create a Key Frame Animation and specify the keyPath attribute of the animation.
- Use the beiser curve as the animation execution path.
- Add an animation to a specified layer
Implementation steps (subclass of custom UIView)
- Save the besher path using the member attributes
@property (nonatomic, strong) UIBezierPath *path;
Listen to the status of the touch event and draw the beser Curve
// Determine the start point-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// obtain the current touch point UITouch * touch = [touches anyObject]; CGPoint curretnPoint = [touch locationInView: self]; // create path UIBezierPath * path = [UIBezierPath bezierPath]; [path moveToPoint: curretnPoint]; // save path self. path = path ;}
// Add a line-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {// obtain the current touch point UITouch * touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView: self]; // Add a line [self. path addLineToPoint: currentPoint]; // redraw the curve and display it on the layer [self setNeedsDisplay];}
- End (create an animation)
-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {// create an animation CAKeyframeAnimation * animation = [CAKeyframeAnimation animation]; // specify the attributes of the animation, animation. keyPath = @ "position"; // sets the animation execution path, animation. path = self. path. CGPath; // set the animation execution time to animation. duration = 1; // set the animation repetition rate to animation. repeatCount = MAXFLOAT; // Add the animation to the corresponding layer [[self. subviews firstObject] layer] addAnimation: animation forKey: nil];}
Show the path to the Layer
// Draw the path-(void) drawRect :( CGRect) rect {[self. path stroke];}