There are two ways to implement animations in iOS, one is uiview to animate, the other is implemented by Calayer, and the following is a simple implementation of two animations:
First, the realization of UIView animation
UIView using context for animation
Key code:
Parameter 1 animation name Parameter 2 object context to implement animation [UIView beginanimations:@ "attribute" context:_showimageview]; // Set the animation time [UIView setAnimationDuration:1.0f]; //setting animation delay Time// [uiview setanimationdelay:2]; //set view center implementation try to move animation _showImageView.center = Cgpointmake (100, 100); //Set alpha value: View Transparency _showimageview.alpha = 0.2f; //Setting the background color _showImageView.backgroundColor = [UIColor greenColor]; //uiview Animation Setup Proxy [uiview setanimationdelegate: self];  The //animation will begin the agent method [uiview setanimationwillstartselector: @selector ( Animationwillstart:context:)]; //animation has ended proxy method [uiview setanimationdidstopselector: @selector (animationDidStop:finished:context:)]; //Submit animation settings, perform animations [UIView commitAnimations];
Animations that are implemented using block:
UIView Animation, using block implementation [uiview animatewithduration:1.0f animations:^{ // To implement the offset of the view by setting the translation if ([Self.myswitch ison] ) { //based on the last translation _showimageview.transform = cgaffinetransformtranslate (_ SHOWIMAGEVIEW.TRANSFORM, 50, 0); } else { //based on the original translation _showimageview.transform = Cgaffinetransformmaketranslation ( -50, 0); } }];
Second, the realization of Calayer animation
Cabasic animations: Determining animations based on initial position and end position
Cabasic has two properties Fromvalue animation start value, tovalue animation end value Cabasicanimation *animation1 = [cabasicanimation animationwithkeypath:@ ' P Osition "]; [Animation1 Setduration:2]; Animation1.fromvalue = [Nsvalue valuewithcgpoint:cgpointmake (150, 150)]; Animation1.tovalue = [Nsvalue valuewithcgpoint:cgpointmake (200, 200)]; [_imageview.layer addanimation:animation1 forkey:@ "position"];
Create a set of animations:
Create a group animation object CAAnimationGroup *group = [CAAnimationGroup animation]; //cabasic Animation cabasicanimation * animation1 = [cabasicanimation animationwithkeypath:@ "Transform.scale.y"]; animation1.fromValue = @1.5; animation1.toValue = @0.5; //Key-Frame animation cakeyframeanimation *animation2 = [cakeyframeanimation animationwithkeypath:@ "Position"]; Animation2.values = @[[nsvalue valuewithcgpoint:cgpointmake (100, 100)], [nsvalue valuewithcgpoint:cgpointmake (200, 150)], [nsvalue Valuewithcgpoint:cgpointmake (100, 200)], [NSValue Valuewithcgpoint:cgpointmake (200, 250)]]; //group add an animated array, Concurrent execution of animated objects in group [group setAnimations:@[animation1, animation2]]; [group setduration:4.0f]; [_imageview.layer addanimation:group forkey:@ "group"];
Complete engineering and code see:https://github.com/winann/iOS-Animation
The animation of the project in the implementation of a comparison of all, and there are comments, we can directly download the project down, while watching the practice.
This article is from the "A Mao" blog, please be sure to keep this source http://winann.blog.51cto.com/4424329/1441537
iOS animations: UIView animations and Calayer animations (use of cabasicanimation, cakeyframeanimation)