Core Animation and Calayer

Source: Internet
Author: User

Animation core must be familiar to everyone, is a set of Apple for animation effect of the good API, directly on the layer. Today I'm going to pull out some of the details of this API, since it's on the layer, let's start with the layer ...

First, Calayer

1.1 What is Calayer

Each UI control that can be seen on the interface is actually a layer within the UI controls. This layer is the Calayer class, it has a very good method:-(void) Drawincontext: (Cgcontextref) CTX, from this can be seen, when the UIView need to display when it is called to draw, After the drawing is finished, the system will copy the layer to the screen and you will be seen. It also has a cool way:-(void) Addanimation: (caanimation *) Anim forkey: (NSString *) key; This is the method associated with animation core, and we'll see

1.2 Calayer How to use? (Calayer is defined in the Quartzcore framework)

 /*  * Layer creation and initialization. *  */ + (instancetype) layer;  /*   the designated initializer.  */- (Instancetype) init;  /*    This initializer was used by coreanimation to create shadow copies of * layers, e.g. for use As presentation layers. Subclasses can override * This method to copy their instance variables into the presentation * layer (subclasses should CA ll the superclass afterwards). Calling this * method in any other situation would result in undefined behavior.  */-(Instancetype) Initwithlayer: (id ) layer; 
View Code

Calayer provides so many ways to create it. A uiview default will have a layer, but you can manually add additional layers, you can set or modify the layer's properties to change the actual appearance. These basics are directly affixed to the code to demonstrate

Calayer *customlayer=[calayer layer];    Customlayer.bounds=cgrectmake (ten);        Customlayer.backgroundcolor=[Uicolor orangecolor]. Cgcolor;         // set the location of the center point of the layer (by default, the center point is 0,0)    Customlayer.position=cgpointmake (+);         // set the fillet radius    customlayer.cornerradius=;    [Self.view.layer Addsublayer:customlayer];
View Code

It is also possible to let ourselves create layers of realistic concrete content, such as a picture (set its Contents property) customlayer.contents= (ID) [UIImage imagenamed:@ "fish"]. Cgimage;

Cgcolorref and Uicolor and Cgimageref and UIImage

As noted above, Calayer is defined in the Quartzcore framework, and CGIMAGEREF, cgcolor are defined in the Coregraphics framework, Uicolor and uiimage are defined in the Uikit framework. Quartzcore and Coregraphics are cross-platform and can be used on both Mac and iOS, but Uikit can only be used in iOS, so quartzcore can only use Cgimageref, Cgcolor for portability. Undenstand?

Second, Core Animation

2.1 Core Animation Basic use

UIView *myview=[[uiview Alloc] Initwithframe:cgrectmake ( -, -, -, -)]; MyView.layer.position=cgpointmake ( -, -); Myview.backgroundcolor=[Uicolor Orangecolor]; Self.myview=MyView;        [Self.view Addsubview:myview]; Cabasicanimation*anim=[cabasicanimation Animationwithkeypath:@"position"]; Anim.duration=1.5; Anim.fromvalue=[nsvalue Valuewithcgpoint:cgpointmake ( -, the)]; Anim.tovalue=[nsvalue Valuewithcgpoint:cgpointmake ( -, -)]; Anim.Delegate=Self ; Anim.removedoncompletion=NO; Anim.fillmode=kcafillmodeforwards; [Myview.layer addanimation:anim Forkey:@"Translate"];
View Code

Two properties to be aware of:

1, Tovalue said to move to a location, if replaced by Byvalue will become a move how many positions

2, by default, the animation will return to the original position, in order to maintain the state after the move, you need to set

Anim.removedoncompletion=no;

Anim.fillmode=kcafillmodeforwards;

Cabasicanimation is Capropertyanimation's own class, Capropertyanimation is also the subclass of Caanimation, in Caanimation, there is an agent in this parent class with two proxy methods

-(void) Animationdidstart: (caanimation *) Anim;

-(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag;

can be used to monitor the completion of the movement. In the Animationdidstop: method to print the current position of the UIView, you can see that the position before the motion has not changed. What is this for?

2.2 Core Animation maintenance of two layer

What you need to explain here is that when you add an animation to a layer, it's not directly modifying its properties.

Core Animation maintains two parallel layer hierarchies: The model layer tree and the presentation layer trees, which are models and presentation layers. The layers in the former reflects the state of the layers that we can see directly, while the latter layers is the approximation of the value that the animation is performing.

/* Returns A copy of the layer containing all properties as they were

* At the start of the "transaction", with any active animations

* Applied. This gives a close approximation to the version of the layer

* That's currently displayed. Returns Nil If the layer has not yet

* Been committed.

*

* The effect of attempting to modify, the returned layer in any are

* Undefined.

*

* the ' sublayers ', ' Mask ' and ' Superlayer ' properties of the returned

* Layer return the presentation versions of these properties. This

* carries through to read-only layer methods. e.g., Calling-hittest:

* On the result of The-presentationlayer would query the presentation

* Values of the layer tree. */

-(ID) Presentationlayer;

/* When called on the result of The-presentationlayer method, returns

* The underlying layer with the current model values. When called on a

* Non-presentation layer, returns the receiver. The result of calling

* This method after the transaction that produced the presentation

* Layer has completed is undefined. */

-(ID) Modellayer;

It says that by default the animation will be removed after the end, that is, the animation will not modify the presentation layer after the duration has been exceeded, but will be completely removed, once the animation has been removed presentation layer will return to the value of the model layer, And we didn't change the value of the position during the animation, so you see it back where it was.

In addition to setting Fillmode=kcafillmodeforward to force it to stay in the final state and set removedoncompletion to No to prevent animations from being automatically removed, You can also simply set the UIView position as the animated stop position.

One thing that not many people understand is that if you keep a completed animation on a layer, there is an additional overhead because the renderer is going to do additional painting work. Fortunately, the animated object we created was immediately assigned a copy when it was added to the layer. This feature is useful for reusing animations in multiple view.

Cond......

Core Animation and Calayer

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.