iOS Development cabasicanimation Animation comprehension

Source: Internet
Author: User

1, Calayer Introduction

Calayer is a very similar concept to UIView, as well as backgroundcolor, frame and other similar properties, we can consider UIView as a special kind of calayer. But in fact UIView is the Calayer package, on the basis of calayer to add interactive functions. The display of the UIView must depend on the Calayer.
We can also create a new layer as new view and add it to an existing layer, as well as resize, position, transparency, and so on.
In general, layer can be used for two purposes: the first is the setting of the view related properties, including the fillet, shadow, border and other parameters, the second is to achieve the animation control of the view. So animating a view is essentially animating the view's. Layer.

Classification of 2.CAAnimation

(1) cabasicanimation

Base animation, by setting the starting point, the end point, the time, the animation will move along your set point. Can be seen as a special cakeyframeanimation.

(2) Cakeyframeanimation
Key-Frame Animations

(3) Caanimationgroup
Group animations, supporting multiple cabasicanimation or cakeyframeanimation animations at the same time

Basic Animation Code:

    /** Create Calayer*/Calayer* Testlayer =[[Calayer alloc] init]; Testlayer.backgroundcolor=[Uicolor Greencolor].    Cgcolor; Testlayer.frame= CGRectMake ( -, -, -, -); Testlayer.cornerradius=Ten;    [Self.view.layer Addsublayer:testlayer]; /** Create animations*/cabasicanimation* Testanimiation = [cabasicanimation animationwithkeypath:@"Transform.scale"]; Testanimiation.fromvalue= [NSNumber numberwithfloat:1.0]; Testanimiation.tovalue= [NSNumber numberwithfloat:1.5]; Testanimiation.autoreverses=YES; Testanimiation.fillmode=kcafillmodebackwards; Testanimiation.removedoncompletion=NO; Testanimiation.repeatcount=maxfloat; Testanimiation.duration=1; [Testlayer addanimation:testanimiation Forkey:@"testanimiation"];

Parameter explanation:

The values for 1.animationWithKeyPath are as follows:

anchorpoint
backgroundcolor
backgroundfilters
bordercolor
borderWidth
bounds
compositingfilter
Contents
contentsrect
Cornerradius
doublesided
Filters
Frame : This property was not animatable. You can achieve the same results by animating theboundsandpositionproperties.
Hidden
Mask
maskstobounds
opacity
Position
Shadowcolor
shadowoffset
shadowopacity
Shadowpath
Shadowradius
sublayers
sublayertransform
Transform flip contains scale rotation
zposition

Most of what we commonly use is:

Transform.scale = scaled animation
transform.scale.x = wide animation of proportions
Transform.scale.y = High Proportional animation
Transform.rotation.z = rotation of the plane
opacity = Transparency

2.fromValue: The starting state value of the animation, although the type given by the iOS document is id , it should be passed NSValue to objects such as NSNumber (Nsnubmer inherits from Nsvalue). Its specific meaning

3.AutoReverse: When the animation is executed to the state specified by Tovalue is back from the state of Tovalue, or jump directly to the state of Fromvalue and then execute again

4.filemode:fillmode's role is to determine the behavior of the current object over a non-active time period. A non-active time period is before the animation starts and after the animation ends. If it is an animated caanimation, you need to set its removedoncompletion to No, otherwise fillmode will not work. Here are the meanings of each fillmode:

Kcafillmoderemoved This is the default value, that is, when the animation starts and after the end of the animation, the animation has no effect on the layer, the end of the animation, the layer will revert to the previous state

Kcafillmodeforwards when the animation is finished, the layer will remain the last state of the animation

Kcafillmodebackwards This is relative to Kcafillmodeforwards, just before the animation starts, you just add the animation to a layer,layer and immediately enter the initial state of the animation. Because it is possible that Fromvalue is not the initial state of the current layer, if Fromvalue is the current state of the layer, then this parameter does not make much sense.

Kcafillmodeboth understand the above two, this is very good understanding, this is actually the above two synthesis. Before the animation joins, the layer is in the initial state of the animation, and the layer retains the last state of the animation after the animation is finished.

Caanimationgroup Implementing animations

 /** Create Calayer*///Calayer * Testlayer = [[Calayer alloc] init];//Testlayer.backgroundcolor = [Uicolor Greencolor]. Cgcolor;//testlayer.frame = CGRectMake ( +, +, +);//Testlayer.cornerradius = ten;//[Self.view.layer Addsublayer:testlayer];UILabel * label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Label.backgroundcolor=[Uicolor Greencolor];    [Self.view Addsubview:label]; /** Create animations*/    /** Zoom*/cabasicanimation* Testanimiation = [cabasicanimation animationwithkeypath:@"Transform.scale"]; Testanimiation.fromvalue= [NSNumber numberwithfloat:1.0]; Testanimiation.tovalue= [NSNumber numberwithfloat:1.5]; Testanimiation.autoreverses=YES; Testanimiation.fillmode=kcafillmodebackwards; Testanimiation.removedoncompletion=NO; Testanimiation.repeatcount=maxfloat; Testanimiation.duration=1;//[Label.layer addanimation:testanimiation forkey:@ "Testanimiation"];    /** Mobile*/cabasicanimation*moveanimation = [Cabasicanimation animationwithkeypath:@"position"]; Moveanimation.fromvalue=[Nsvalue valueWithCGPoint:label.layer.position]; Moveanimation.tovalue= [Nsvalue Valuewithcgpoint:cgpointmake ( the- the, LABEL.LAYER.POSITION.Y)]; Moveanimation.autoreverses=YES; Moveanimation.repeatcount=maxfloat; Moveanimation.duration=2; /** Rotation*/cabasicanimation*rotateanimation = [Cabasicanimation animationwithkeypath:@"transform.rotation.x"]; Rotateanimation.fromvalue= [NSNumber numberwithfloat:0.0]; Rotateanimation.tovalue= [NSNumber numberwithfloat:6.0*M_PI]; Rotateanimation.autoreverses=YES; Rotateanimation.repeatcount=maxfloat; Rotateanimation.duration=2; /** Animation Combination*/Caanimationgroup*groupannimation =[Caanimationgroup animation]; Groupannimation.duration=2; Groupannimation.autoreverses=YES; Groupannimation.animations=@[moveanimation, Testanimiation, rotateanimation]; Groupannimation.repeatcount=maxfloat; [Label.layer addanimation:groupannimation Forkey:@"groupannimation"];

iOS Development cabasicanimation Animation comprehension

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.