iOS animation--coreanimation

Source: Internet
Author: User

Coreanimation
In my previous Uikit animation simple to mention a coreanimation animation, in fact, we do not look at its class library name species have a animation, in fact, animation in this library only occupies a small position.

These properties, such as the Borders, fillets, shadows, anchors, and so on that we often use, are provided by the CA.

Before we say the CA animation, let's talk about the Calayer class, Calayer can be called a layer, UIView is a view. The biggest difference between Calayer and UIView is that Calayer does not handle interactions, such as click events.

In fact, UIView is calayer high-level package, UIView reality, drawing, animation and so on are encapsulated Calayer, in Calayer has an important attribute is called affinetransform (radiation Transformation), In fact, it and UIView in the transform is a touch of the same, the return is

Caaffinetransform. When you change a View.transform property or view.layer.transform You need to restore the default state, remember to reset them to use

View.transform = cgaffinetransformidentity, or View.layer.transform =catransform3didentity, Assuming you're constantly changing a view.transform property, and you don't reset it before each change, you'll see that the changes you've made and what you want are changing, not the results you really want.

Now that we have a preliminary understanding of Calayer, let's take a look at how the animation is used in the CA.

Implicit animations

In fact, we just need to modify the Calayer and animation-related properties, even if we do not write animation code, in the run time will also have animation effect. This is called implicit animation, which means we don't have a noticeable call.

Let's look at an example

@interfaceViewcontroller () {Calayer*_viewlayer;}@end@implementationViewcontroller- (void) viewdidload{[Super Viewdidload]; _viewlayer=[Calayer layer]; _viewlayer.frame= CGRectMake (0,0, -, -); [_viewlayer Setbackgroundcolor:[uicolor Greencolor].    Cgcolor]; _viewlayer.opacity=0.25;        [Self.view.layer Addsublayer:_viewlayer]; //additional setup after loading the view, typically from a nib.}-(Ibaction) Click: (ID) sender{cgaffinetransform movetransforam= Cgaffinetransformmaketranslation ( the, $);    [_viewlayer Setaffinetransform:movetransforam]; _viewlayer.opacity=1;}

Enter this code, click the button, you will find that the view is gradually becoming opaque, and the location has changed.

An explicit animation

In an explicit animation, you do not need to modify the properties and invoke the animation execution method, only need to be defined by cabasicanimation, each object has a duration, number of repetitions and other properties, and then use Addanimation:forkey: method to use each animation in a specific property of the layer, respectively.

The ability to animate layer properties has the following:

Opacity

Transform.scale;

transform.scale.x;

Transform.scale.y;

Transform.rotation.z;

Opacity

Margin

Zposition;

BackgroundColor;

Cornerradius;

Bounds

Contents

Contentsrect;

Frame

Hidden

Mask

Maskstobounds;

Position

Shadowcolor;

Shadowoffset;

shadowopacity;

Shadowradius;

Let's look at an example of an explicit animation:

Cabasicanimation *opanim = [cabasicanimation animationwithkeypath:@"Opacity"]; Opanim.duration=3.0; Opanim.fromvalue= [NSNumber numberwithfloat:. -]; Opanim.tovalue= [NSNumber numberwithfloat:1.0]; Opanim.cumulative=NO; Opanim.repeatcount=2; Opanim.fillmode=kcafillmodeforwards; Opanim.removedoncompletion=NO; [_viewlayer addanimation:opanim Forkey:@"animateopacity"]; Cgaffinetransform Movetransform= Cgaffinetransformmaketranslation ( the, $); Cabasicanimation*moveanim = [Cabasicanimation animationwithkeypath:@"Transform"]; Moveanim.duration=6.0; Moveanim.tovalue=[Nsvalue Valuewithcatransform3d:catransform3dmakeaffinetransform (Movetransform)]; Moveanim.fillmode=kcafillmodeforwards; Moveanim.removedoncompletion=NO; Moveanim.Delegate=Self ; Moveanim.repeatcount=2; [_viewlayer addanimation:moveanim Forkey:@"Animatetransform"];

Replacing the code in the click with the code above will reveal that the view is slowly becoming opaque and executed two times in the process.

Here's a look at what the Cabasicanimation property does:

Property Describe
Duration Animation duration
Fromvalue Animation's property start value
Tovalue Property target value for animation
Cumulative Whether to retain the value of the last animation
RepeatCount Number of repetitions
Fillmode Do not return to the original position after using animation
Removeoncompletion Remove animation state after animation finishes
Delegate Animation agent

Here is a brief cumulative, the above example if cumulative is set to Yes, the second opacity change is not performed because the state of the animation is preserved.

Fillmode and removeoncompletion work together so that the view ends when the animation ends, otherwise the view goes back to its original position.

But here's to say, although we see the value has changed, but in fact the original value is not because of the animation and change, but is affinetransform change, if you want to do affinetransform as above said, need to let it restore the initial value will not be a problem.

Then look at delegate, but the agent is not cabasicanimation, nor its parent capropertyanimation, but its parent class Caanimation!

The proxy method is as follows

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

One is that the animation is the callback at the beginning of the animation, and the other is the callback at the end of the animation.

Key-Frame Animations

Keyframe animation can also be said to be divided into two, one is a frame animation is a path animation.

Frame animation, not much to say first look at the example

Cakeyframeanimation *opanim = [cakeyframeanimation animationwithkeypath:@"Opacity"]; Opanim.duration=6.0; Opanim.values= [Nsarray arraywithobjects:[nsnumber numberwithfloat:0.25],[nsnumber numberwithfloat:0.75],[nsnumber numberwithfloat:1.0], nil]; Opanim.keytimes= [Nsarray arraywithobjects:[nsnumber numberwithfloat:0.0],[nsnumber numberwithfloat:0.5],[nsnumber numberwithfloat:1.0], nil]; Opanim.fillmode=kcafillmodeforwards; Opanim.removedoncompletion=NO; Opanim.Delegate=Self ; [_viewlayer addanimation:opanim Forkey:@"animateopacity"];

Here we animate the transparency, setting in values and keytimes a value that should arrive at a certain time, which affects the state of the animation at different times. You should remember that in previous Uikit animations, the meaning of Keyframe start and duration was described, and they were all percentages of total time.

Path animation

The path animation can be said to be a special frame animation, the first animation object property is position, and then the rate of each frame is the same, with the concept of multithreading is probably the meaning of serial execution animation.

Cgmutablepathref Path =cgpathcreatemutable (); Cgpathmovetopoint (Path, NULL, the. F, -. f); Cgpathaddlinetopoint (Path, NULL, -. F,280. f); Cgpathaddlinetopoint (Path, NULL,260. F, the. f); Cgpathaddlinetopoint (Path, NULL, -. F, the. f); Cgpathaddlinetopoint (Path, NULL, -. F,280. f);    Cgpathclosesubpath (path); Cakeyframeanimation*anim = [Cakeyframeanimation animationwithkeypath:@"position"]; [Anim setduration:Ten. f];    [Anim setdelegate:self];    [Anim Setpath:path];    Cfrelease (path); Path=Nil; [_viewlayer addanimation:anim Forkey:@"position"];

here with Cgmutablepathref to produce the path context, in fact cgmutablepathref and drawing cgcontextref similar.

The following settings are no longer for values and keytimes, but instead set its Path property.

Finally, do not forget to release cgmutablepathref, because it is the Corefoundation object does not use arc, so to manually manage memory.

iOS animation--coreanimation

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.