Cakeyframeanimation-Key Frame Animation and animation group ~

Source: Internet
Author: User
• Key Frame Animation is also a subclass of capropertyanimation. The difference with cabasicanimation is that-cabasicanimation can only be changed from one value (fromvalue) to another value (tovalue ), cakeyframeanimation uses an nsarray to save these values-• attribute description:-values: the above nsarray object. The elements are called "key frames" (keyframe ). The animation object will display each key frame-path in the values array in sequence within the specified time (duration): You can set a cgpathref and cgmutablepathref to move the layers according to the Path. Path only applies to the anchorpoint and position of calayer. If path is set, values will be ignored-keytimes: You can specify the corresponding time point for the corresponding key frame. The value range is 0 to 1.0, each time value in keytimes corresponds to each frame in values. If keytimes is not set, the time of each key frame is evenly divided • cabasicanimation can be viewed as a cakeyframeanimation with only two key frames ================== =======• there is also a very important parameter in the Key Frame Animation, that is, calculationmode, the so-called calculation mode: It mainly targets the situation where the content of each frame is a coordinate point, that is, an animation of anchorpoint and position • when there are multiple discrete points in a plane coordinate system, they can be discrete, or they can be connected in a straight line for interpolation calculation, you can also use smooth curves to connect them for interpolation • calculationmode currently provides the following modes:-kcaanimationlinear default value, indicating that when the key frame is a coordinate point, key Frames are directly connected to each other for interpolation computation-kcaanimationdiscrete discretization without interpolation computation. All key frames are displayed one by one.-kcaanimationpaced allows the animation to run evenly, keytimes and timingfunctions are invalid.-kcaanimationcubic performs smooth curve connection and Interpolation Calculation for key frames whose key frames are coordinate points, the main purpose here is to make the running track smooth-kcaanimationcubicpaced you can see the name and there is a certain relationship with kcaanimationcubic, in fact, it is to make the animation run even on the basis of kcaanimationcubic, that is, the distance between the motion in the system time is the same. At this time, keytimes and timingfunctions are invalid. • Note: currently, the study of this attribute is of little significance. You only need to know that there is such an attribute, only when complex animations are done and the animation effect is not ideal, you need to consider using this property ================================================== =========== after the Key Frame Animation is added to the path point, the intermediate state is automatically calculated. The following is a simple demo of adding frame animations.
// Add an intermediate point // use a random point in the center. // 1. animation cakeyframeanimation * anim = [cakeyframeanimation animationwithkeypath: @ "position"]; nsvalue * P1 = [nsvalue valuewithcgpoint: Self. center]; nsvalue * P2 = [nsvalue valuewithcgpoint: [self randompoint]; nsvalue * P3 = [nsvalue valuewithcgpoint: To]; anim. values = @ [P1, P2, P3]; anim. duration = 1.0f; anim. removedoncompletion = no; anim. fillmode = kcafillmodeforwards; anim. delegate = self; [self. layer addanimation: anim forkey: Nil];

 

The following is a demo of shaking animation with frame animation.

-(Void) Shake {// 1. animation cakeyframeanimation * anim = [cakeyframeanimation animationwithkeypath: @ "transform. rotation "]; // 2. set the angle cgfloat angle = m_pi_4/10; anim. values = @ [@ (-angle), @ (angle), @ (-angle)]; anim. duration = 0.2f; anim. repeatcount = huge_valf; [self. layer addanimation: anim forkey: Nil];}

 

The Key Frame Animation can also be animated along the cgpath path.

# Pragma mark-Key Frame Animation translated by path-(cakeyframeanimation *) movewithpath :( cgpathref) path duration :( nstimeinterval) Duration {// 1. animation cakeyframeanimation * anim = [cakeyframeanimation animationwithkeypath: @ "position"]; // 2. set the path anim. path = path; // 3. set the duration anim. duration = duration; return anim ;}

 

 

An animation group is the synthesis of a group of animations. It can merge multiple animations and add them to layers to allow multiple animations to be executed simultaneously.

Below is a small demo of an animation Group

-(Void) groupdemo {/** a Group Animation can inherit a group of animations and execute them concurrently to produce more complex animation effects. Note that the animations in a group, you cannot modify the same keypath * // instantiate an animation group caanimationgroup * Group = [[caanimationgroup alloc] init]; // transparency animation cabasicanimation * alpha = [cabasicanimation animationwithkeypath: @ "opacity"]; Alpha. fromvalue = @ (1.0); Alpha. tovalue = @ (0.5); // rotate the animation cabasicanimation * rotate = [cabasicanimation animationwithkeypath: @ "transform. rotation "]; rotate. tovalue = @ (2 * m_pi); // Key Frame Animation cakeyframeanimation * keyanim = [cakeyframeanimation animationwithkeypath: @ "position"]; cgmutablepathref Path = equals (); equals (path, null, cgrectmake (100,100,200,300); keyanim. path = path; // release path cgpathrelease (PATH); group. animations = @ [Alpha, keyanim, rotate]; group. duration = 1.0f; [_ myview. layer addanimation: group forkey: Nil];}

 

 

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.