Go: iOS animations

Source: Internet
Author: User

iOS animations

Original link: http://www.cnblogs.com/zzuliliu/p/5468732.html

This article mainly explains the use of animations in iOS.

1, Core Animation, key animation.

The core animation process is performed in the background, and does not block the main thread. Core Animation is directly acting on calayer, not uiview.

Caanimation is the parent class of all Core Animation animation classes, Caanimation is an abstract class that cannot be used directly and should use its subclasses. Caanimation commonly used subclasses have the following three:

Capropertyanimation, property animation

Catransition, transitions Animation

Caanimationgroup, group animation

Among them, Capropertyanimation is also an abstract class, we generally use its two sub-classes:

Cabasicanimation, basic animation,

Cakeyframeanimation, frame animation

  There is also a protocol: camediatiming.

1.1,cabasicanimation, basic animation. From the above introduction, we can know that the core animation is the role of the Calayer layer, so the underlying animation is to change the object layer of a property to achieve animation effect. As for the properties of Calayer, you can see the API for yourself, and I'll show you the animation effect only on the position property. Here's a look at the code:

1-(Ibaction) Cabasicanimation: (UIButton *) Sender {2     //parameter needs to be a layer attribute, or the property's property name is 3     cabasicanimation *basic = [ Cabasicanimation animationwithkeypath:@ "position.x"]; 4     basic.fromvalue = @100; 5     basic.tovalue = @250; 6     basic.duration = 3; 7     basic.repeatcount = 1; 8     //Required To manually change the position (if you need to change the real position of the position) 9//    self.redView.layer.position = Cgpointmake (+),     //key, Mark     [ Self.redView.layer Addanimation:basic forkey:nil];12}

To explain the above code, LINE3 creates an animation that changes the position of an object in the x-axis, that is, position.x, and Line4 the start position of X, which specifies the end position of X in Line5. Line6 set the animation execution time to 3s,line7 set the animation repeat number to 1,line11, assign this animation to Self.redView.layer.

When the above animation is done, we will find that the Self.redview is back to the initial position, that is, the animation does not really change the property value of the layer. We can actually change the property value of the layer by using the code in Line9, so that the object stops at the terminating position.

1.2, cakeyframeanimation, key frame animation. This is very similar to the basic animation, but can be done by adding a set of basic animation, the following directly on the code, the code to explain the reference above:

 1//Keyframe Animation 2-(Ibaction) Keyframeanimation: (UIButton *) Sender {3 4 cakeyframeanimation *keyframeanimation = [CAK Eyframeanimation animationwithkeypath:@ "position"]; 5 Cgpoint point0 = cgpointmake (100, 100); 6 Cgpoint point1 = Cgpointmake (100, 200); 7 Cgpoint Point2 = Cgpointmake (200, 200); 8 Cgpoint Point3 = Cgpointmake (200, 300); 9 Cgpoint point4 = Cgpointmake (n.), Cgpoint point5 = Cgpointmake (175, +); Cgpoint point6 = Cgpoint Make (175), nsvalue *value1 = [Nsvalue valuewithcgpoint:point0];14 nsvalue *value2 = [Nsvalue Valuewi thcgpoint:point1];15 nsvalue *value3 = [Nsvalue valuewithcgpoint:point2];16 nsvalue *value4 = [NSValue ValueWithCG point:point3];17 nsvalue *value5 = [Nsvalue valuewithcgpoint:point4];18 nsvalue *value6 = [NSValue valueWithCGPoin t:point5];19 nsvalue *value7 = [Nsvalue valuewithcgpoint:point6];20 21//Create animation according to a set of points Keyframeanimation.va Lues = @[value1, value2, Value3, Value4, Value5, Value6, Value7, value1];23 keyframeanimation.duration = 10;24 [Self.redView.layer addanimation:ke Yframeanimation forkey:nil];25}

The above code, you can create a view on their own, that is, the code in the Self.redview, a demonstration to understand.

1.3 Catransition Transitions Animation. Here's a look at the code:

-(Ibaction) Transitionanimation: (UIButton *) Sender {        catransition *transition = [catransition animation];    Transition.type = @ "Rippleeffect";  Animation transition type    transition.subtype = kcatransitionfromleft;//animation over direction    transition.repeatcount = Huge_vall;//animation repetition times, Maximum number of    transition.duration = 2;    Duration of animation    [Self.redView.layer addanimation:transition forkey:nil];}

Below I listed some of the transition types for your reference use:

1      pagecurl            up one page 2      pageuncurl          down 3      rippleeffect        drip effect 4      suckeffect          Shrinkage Effect , such as a piece of cloth was pumped 5      cube                cube effect 6      oglflip             upside down effect

1.4 Caanimationgroup. Group animation is actually the ability to add several of the above animation types to an object in a casual combination. Here's a look at the code:

 1-(Ibaction) Groupanimation: (UIButton *) Sender {2//Create a scaling animation 3 cabasicanimation *basic = [Cabasicanimation anim ationwithkeypath:@ "Transform.scale"]; 4 Basic.fromvalue = @1; 5 Basic.tovalue = @0.75; 6//Create a keyframe animation from one position to another 7 cakeyframeanimation *keyframeanimation = [cakeyframeanimation animationwithkeypath:@ "p Osition "]; 8 Cgpoint point1 = cgpointmake (100, 100); 9 Cgpoint Point2 = Cgpointmake (+), nsvalue *value1 = [Nsvalue valuewithcgpoint:point1];11 nsvalue *va Lue2 = [Nsvalue valuewithcgpoint:point2];12 keyframeanimation.values = @[value1, value2];13//Create an animation group CAAnim Ationgroup *group = [Caanimationgroup animation];15 group.duration = 4;16 Group.repeatcount = 3;17//Adds two of the above animations Add to animation group, the animation will be executed synchronously [group SETANIMATIONS:@[BASIC,KEYFRAMEANIMATION]];19//The following code will eventually change the position of the object, otherwise the object will return to its original position after the animation is complete. Sel F.redview.layer.position = Cgpointmake (300, 300); 21//Add animation Group [Self.redView.layer Addanimation:group Forkey:NIL];23} 

1.5 caspringanimation, spring animation, inherits from Cabasicanimation. is not difficult, on the code bar:

-(Ibaction) Springanimation: (UIButton *) Sender {        caspringanimation *spring = [caspringanimation animationwithkeypath:@ "Transform.rotation"];    The larger the mass, the greater the inertia    spring.mass = 1;    Damping coefficient, the greater the Stop the faster    spring.damping = 0.2;    Spring.fromvalue = @0.2;//transform.rotation initial value    Spring.tovalue = @0.75;//transform.rotation End Value    Spring.repeatcount = 1;    Spring.autoreverses = yes;//automatic rebound    //initial speed    spring.initialvelocity = 5;    Spring.duration = spring.settlingduration;    [Self.redView.layer addanimation:spring forkey:@ "Spring"];}

Below I summed up the keypath value, we can refer to:

Transform.scale = Proportional Conversion transform.scale.x = width Proportional conversion transform.scale.y = high proportional conversion transform.rotation.z = rotation of the plane graph opacity = transparency m Argin zposition backgroundcolor    background color Cornerradius    fillet borderWidth bounds contents contentsrect Cornerradius Frame Hidden mask maskstobounds opacity position Shadowcolor shadowoffset shadowopacity Shadowradius

Above is the core animation of all the content, the basic is to explain in code, these things on the principle of nothing to say, mainly know how to use. Let's talk about UIView animation.

2, UIView animation. Uikit integrates animations directly into the UIView class, and UIView provides animated support for these changes when some of the internal properties change. The work of performing the animation is done automatically by the UIView class, but you still want to notify the view when the animation is performed, so you need to place code that alters the property in [UIView Beginanimations:nil Context:nil] and [UIView Commitanimations].

  In general, properties that affect the shape of a control can be animated , and UIView animations are implemented in two ways, and we look at the first way, the code below, an animation that changes the control frame:

1-(Ibaction) Changeframe: (UIButton *) Sender {2     ///first parameter, animated 3     [UIView beginanimations:@ "Changeframe" context : nil]; 4     //Set animation number 5     [UIView setanimationrepeatcount:100]; 6     //Set Animation duration 7     [UIView Setanimationduration:1]; 8     //Set animation rate 9     [UIView setanimationcurve:uiviewanimationcurveeaseinout];10     cgrect frame = self.showview.frame;11     FRAME.ORIGIN.Y = 200;12     //change frame,y value     self.showView.frame = frame;14     //Start animation     [UIView COMMITANIMATIONS];16}

You should be able to understand the meaning of this code by reading the comments.

Another piece of code, scaling the animation:

1//Zoom Animation 2-(Ibaction) Scalebuttonaction: (UIButton *) Sender {3     4     [UIView beginanimations:@ "UIView" context: nil];5     self.showView.transform = Cgaffinetransformscale (cgaffinetransformidentity, 0.5, 1.5); 6}

Rotate Animation:

1//Rotation 2-(ibaction) Rotatebuttonaction: (UIButton *) Sender {3     [UIView beginanimations:nil Context:nil]; 4     //First Parameters: Who is the benchmark for rotation 5     //second parameter: rotation of radians 6     //cgaffinetransformidentity Original deformation 7     [UIView Setanimationduration:1]; 8     self.showView.transform = cgaffinetransformrotate (Self.showView.transform,-m_pi/180 *); 9     [UIView commitanimations];10}

Let's look at another way to implement the UIView animation.

A simple block animation:

1//Simple block animation 2-(Ibaction) Simpleblock: (UIButton *) sender{3     [UIView animatewithduration:2 animations:^{4         Cgpoint point = Self.planeImageView.center; 5         Point.y + + + 6         self.planeImageView.center = point; 7     } completion:^ (BOOL finished) {8         NSLog (@ "End of animation "); 9         [UIView animatewithduration:2 animations:^{10             cgpoint point = self.planeimageview.center;11             Point.y-= 100;12             self.planeImageView.center = point;13         } completion:nil];14     }];16}

Complex block animations:

1//Complex Block2-(ibaction) Complexblock: (UIButton *) Sender {3     [UIView animatewithduration:2 delay:0 options: Uiviewanimationoptioncurveeaseinout animations:^{4         self.planeImageView.transform = cgaffinetransformrotate ( Self.planeImageView.transform, m_pi/180 *); 5     } completion:^ (BOOL finished) {6         //revert to initial deformation 7//        Self.planeImageView.transform = cgaffinetransformidentity;8     }];9}

Transition animations:

1//Over animation, transition Animation 2-(Ibaction) Transitionanimation: (UIButton *) Sender {3     [UIView beginanimations:nil context:nil]; 4
   [uiview Setanimationduration:2]; 5     //First parameter: type of transition animation 6     //second parameter: which object to add Transition animation 7     //Third parameter: Cache setting (yes execution more efficient) 8     [UIView setanimationtransition : Uiviewanimationtransitionflipfromleft ForView:self.label Cache:yes]; 9     Self.label.text = @ "Bear bears two Stories";     [UIView commitanimations];11}

Spring Animation:

1//Spring Animation 2-(Ibaction) Sprintanimation: (UIButton *) Sender {3     //1. Animation duration 4//2     . Delay Time 5     //3. Damping coefficient, the larger the spring deformation, the more Small 6     //4. Speed 7     //5. Animation type option 8     [UIView animatewithduration:2 delay:0 usingspringwithdamping:0.1 INITIALSPRINGV Elocity:10 options:uiviewanimationoptioncurvelinear animations:^{9         cgpoint point = Self.planeImageView.center;         Point.y + = 260;11         Self.planeImageView.center = point;12         +     completion:^ (BOOL finished) {14         NSLog (@ "End of animation");     16}

As you can see, the second implementation is actually using the block package for the first way, which is relatively convenient for the second implementation approach. While looking closely at the core animation and UIView animation, we can also find that, in fact, the UIView animation is the core animation encapsulation, the use of more convenient.

Go: iOS animations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.