We introduced the core animation basics in the previous section, which describes the main applications and limitations of core animations.
"Limitations of core animations"
One thing to note, all the core animation is the illusion, just modified the view display position, and can not modify the real position, even if the setting is no longer, as if the position moved, in fact, the layer is still in place, to prove this point, you can print the location of the layer after the animation is finished.
In order to get the animation end time, through the agent, UIView has followed the relevant protocol, can achieve the beginning and end of the animation two methods.
-(void) Animationdidstart: (caanimation *) anim{ }-(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag{ }
We can customize a view, then change its position, in the Animationdidstop:: Method to print its location, you can find that the location is not really changed.
"Transition Animation"
In this way, with the core animation to change the core location of the view is not appropriate, the core animation is generally used only to do transition animation, such as the switch of multiple images, if we have 1, 2, 33 pictures, the default is the first one, every click on the screen to switch one, with the following code can be set to toggle animation.
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{ static int clickcnt = 2; NSString *imagename = [NSString stringwithformat:@ "%d", clickcnt++]; if (clickcnt > 3) clickcnt = 1; _imageview.image = [UIImage imagenamed:imagename]; Catransition *anim = [catransition animation]; Anim.duration = 1.0; Anim.type = @ "Cube"; [_imageview.layer Addanimation:anim Forkey:nil]; }
the Anim type has several settings, and there are subtype under type, such as page flipping properties, which can be set for page flipping direction:
Anim.type = @ "Pagecurl"; anim.subtype = Kcatransitionfromleft;
Alternatively , you can set the type directly from the system-defined const:
Anim.type = Kcatransitionreveal;
"Animation Group"
the core animation described in the previous section can only perform one animation at a time, in order to be able to perform multiple animations at once, just use Caanimationgroup Wraps all the animations, and to set the no-bit only needs to set the group's properties, such as the following code, you can implement three animations simultaneously.
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{ caanimationgroup *anims = [Caanimationgroup Animation]; Cabasicanimation *rotation = [cabasicanimation animation]; Rotation.keypath = @ "Transform.rotation"; Rotation.tovalue = @M_PI_2; Rotation.duration = 0.25; Cabasicanimation *move = [cabasicanimation animation]; Move.keypath = @ "position"; Move.tovalue = [Nsvalue valuewithcgpoint:cgpointmake (0)]; Move.duration = 0.5; Cabasicanimation *scale = [cabasicanimation animation]; Scale.keypath = @ "Transform.scale"; Scale.tovalue = @0.5; Scale.duration = 0.5; Anims.animations = @[rotation, move, scale]; Anims.removedoncompletion = NO; Anims.fillmode = kcafillmodeforwards; [_blueview.layer addanimation:anims Forkey:nil]; }
"UIView's animation"
Remove the use of the core animation, you can directly use the UIView animation, by setting the parameters can also achieve the role of the core animation, and can really change the location of the view. Use the following method to animate the content in animations by setting the animation type from the options.
[UIView transitionwithview:<# (UIView *) #> duration:<# (nstimeinterval) #> options:<# ( uiviewanimationoptions) #> animations:<#^ (void) animations#> completion:<#^ (BOOL finished) completion# ;]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(122) Core Animation advanced