Transfer animation CATransition and Group Animation CAAnimationGroup,
I. Brief Introduction to transition Animation
The subclass of CAAnimation, used for transfer animation, can provide animation effects for the layer to remove the screen and move into the screen. IOS has less animation effect than Mac OS X.
UINavigationController is an animation that pushes the Controller view into the screen through CATransition.
Property parsing:
Type: animation transition type
Subtype: animation transition direction
StartProgress: animation start point (percentage in the overall animation)
EndProgress: animation end point (percentage in the overall animation )?
Ii. Transfer animation code example
1. Build the interface
2. Implementation Code
# Import "YYViewController. h "@ interface YYViewController () @ property (nonatomic, assign) int index; @ property (weak, nonatomic) IBOutlet UIImageView * iconView;-(IBAction) preOnClick :( UIButton *) sender; -(IBAction) nextOnClick :( UIButton *) sender; @ end @ implementation YYViewController-(void) viewDidLoad {[super viewDidLoad]; self. index = 1 ;}- (IBAction) preOnClick :( UIButton *) sender {self. index --; if (self. index <1) {self. index = 7;} self. iconView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "d.jpg", self. index]; // create the core animation CATransition * ca = [CATransition animation]; // tell the animation to be executed // set the excessive effect ca. type = @ "cube"; // sets the excessive direction of the animation (to the left) ca. subtype = kCATransitionFromLeft; // set the animation time ca. duration = 2.0; // set the starting position of the animation, such as the page turning function, to set the starting position of the page turning anim. startProgress = 0.3; // set the animation end position anim. endProgress = 0.5; // Add an animation [self. iconView. layer addAnimation: ca forKey: nil];} // next-(IBAction) nextOnClick :( UIButton *) sender {self. index ++; if (self. index> 7) {self. index = 1;} self. iconView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "d.jpg", self. index]; // 1. create a core animation CATransition * ca = [CATransition animation]; // 1.1 tell the animation to be executed // 1.2 set the excessive effect ca. type = @ "cube"; // 1.3 sets the excessive direction of the animation (to the right) ca. subtype = kCATransitionFromRight; // 1.4 sets the animation time ca. duration = 2.0; // 1.5 sets the animation start point ca. startProgress = 0.5; // 1.6 sets the animation end point ca. endProgress = 0.5; // 2. add an animation [self. iconView. layer addAnimation: ca forKey: nil];} @ end
Click the previous or next button to display the corresponding animation effect.
Supplement: The UIView animation can also be used for transition animation, which is a little simpler.
[UIView transitionWithView: self. imageV duration: 0.5 options: UIViewAnimationOptionTransitionCurlUp animations: ^ {// transfer code _ I ++; if (_ I = 4) {_ I = 1 ;} NSString * imageName = [NSString stringWithFormat: @ "% d", _ I]; self. imageV. image = [UIImage imageNamed: imageName];} completion: ^ (BOOL finished) {}];
Iii. Simple Description of Group Animation
A subclass of CAAnimation that can save a set of animation objects. After adding the CAAnimationGroup object to the layer, all animation objects in the group can run concurrently.
Property parsing:
Animations: NSArray used to save a set of animation objects
By default, a group of animation objects run simultaneously. You can also set the beginTime attribute of the animation object to change the animation start time.
Iv. Group Animation code example
Code:
1 # import "YYViewController. h "2 3 @ interface YYViewController () 4 @ property (weak, nonatomic) IBOutlet UIView * iconView; 5 6 @ end 7 8 @ implementation NJViewController 9 10-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event11 {12 13 // Pan animation 14 CABasicAnimation * a1 = [CABasicAnimation animation]; 15 a1.keyPath = @ "transform. translation. y "; 16 a1.toValue = @ (100); 17 // scaling animation 18 CABasicAnimation * a2 = [CABasicAnimation animation]; 19 a2.keyPath = @" transform. scale "; 20 a2.toValue = @ (0.0); 21 // rotate the animation 22 CABasicAnimation * a3 = [CABasicAnimation animation]; 23 a3.keyPath = @" transform. rotation "; 24 a3.toValue = @ (M_PI_2); 25 26 // Group animation 27 CAAnimationGroup * groupAnima = [CAAnimationGroup animation]; 28 29 groupAnima. animations = @ [a1, a2, a3]; 30 31 // sets the group animation time 32 groupAnima. duration = 2; 33 groupAnima. fillMode = kCAFillModeForwards; 34 groupAnima. removedOnCompletion = NO; 35 36 [self. iconView. layer addAnimation: groupAnima forKey: nil]; 37} 38 39 @ end
Note: pan-rotate-Zoom is executed together as a group of animations.
Execution result: