iOS basic animation/Keyframe animation/using easing functions for physical animation effects

Source: Internet
Author: User

First, the Basic animation section

Basic Animation section is relatively simple, but can be achieved by the animation effect is also very limited

The use method is roughly:

#1. Create the original UI or screen

#2. Create a Cabasicanimation instance, and set the Keypart/duration/fromvalue/tovalue

#3. Set the position where the animation will eventually stay

#4. Add a configured animation to the layer

For example, to achieve a circle moving from top to bottom, on the code:

1 //set the original screen2UIView *showview = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)];3ShowView.layer.masksToBounds =YES;4ShowView.layer.cornerRadius = -. F;5ShowView.layer.backgroundColor =[Uicolor Redcolor]. Cgcolor;
6 [Self.view Addsubview:showview];7 8 //Create a basic animation9Cabasicanimation *basicanimation =[cabasicanimation animation];Ten One //Setting Properties ABasicanimation.keypath =@"position"; -Basicanimation.duration =4.0f; -Basicanimation.fromvalue =[Nsvalue ValueWithCGPoint:showView.center]; theBasicanimation.tovalue = [Nsvalue valuewithcgpoint:cgpointmake ( -, -)]; - - //Set animation End position -Showview.center = Cgpointmake ( -, -); + - //add animations to layer layers +[Showview.layer addanimation:basicanimation Forkey:nil];

Next, key-frame animations

In fact, similar to the basic animation, just can set up a number of animation path using the same method, roughly

#1. Create the original UI or screen

#2. Create a Cakeyframeanimation instance and set keypart/duration/values you can only set the start and end points for the basic animation, and the Keyframe animation can add multiple animation path points

#3. Set the position where the animation will eventually stay

#4. Add a configured animation to the layer

For example, the red circle is shaking left and right down the code:

1 //set the original screen2UIView *showview = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)];3ShowView.layer.masksToBounds =YES;4ShowView.layer.cornerRadius = -. F;5ShowView.layer.backgroundColor =[Uicolor Redcolor]. Cgcolor;6     7 [Self.view Addsubview:showview];8     9     //Create a keyframe animationTenCakeyframeanimation *keyframeanimation =[cakeyframeanimation animation]; One      A     //Set animation Properties -Keyframeanimation.keypath =@"position"; -Keyframeanimation.duration =4.0f; the      -Keyframeanimation.values =@[[nsvalue ValueWithCGPoint:showView.center], -[Nsvalue Valuewithcgpoint:cgpointmake ( -, -)], -[Nsvalue Valuewithcgpoint:cgpointmake ( -, Max)], +[Nsvalue Valuewithcgpoint:cgpointmake ( $, $)]]; -      +     //Set animation End position AShowview.center = Cgpointmake ( $, $); at      -     //add animations to layer layers -[Showview.layer addanimation:keyframeanimation Forkey:nil];

Finally, we use the easing function with the key frame animation to achieve a more complex physical animation

First of all, what is the easing function, that is, someone who wrote a library can calculate the path of a simulated physical animation (such as a spring effect)

GitHub Address: Https://github.com/YouXianMing/EasingAnimation

Specifically what animation effects can be seen in the library of the easing function query table, simple to give a small ball landing effect

On the code:

1 //set the original screen2UIView *showview = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)];3ShowView.layer.masksToBounds =YES;4ShowView.layer.cornerRadius = -. F;5ShowView.layer.backgroundColor =[Uicolor Redcolor]. Cgcolor;6     7 [Self.view Addsubview:showview];8     9     //Create a keyframe animationTenCakeyframeanimation *keyframeanimation =[cakeyframeanimation animation]; One      A     //Set animation Properties -Keyframeanimation.keypath =@"position"; -Keyframeanimation.duration =4.0f; the     
     //Key, use the easing function here to calculate the point path -Keyframeanimation.values =[yxeasing calculateFrameFromPoint:showView.center -Topoint:cgpointmake ( -, -) - Func:bounceeaseout +Framecount:4.0f* -]; - + //Set animation End position AShowview.center = Cgpointmake ( -, -); at - //add animations to layer layers -[Showview.layer addanimation:keyframeanimation Forkey:nil];

iOS basic animation/Keyframe animation/using easing functions for physical animation effects

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.