15.1 Introduction to Animation
15.2 Core Animation Foundation
15.3 Implicit animations
15.4 Explicit animations
15.5 Key-Frame explicit animations
15.6 UIView Level Animations
15.1 Introduction to animation
The main techniques for animating in iOS are: Core Animation. The Core animation is responsible for all scrolling, rotating, zooming, and zooming, as well as all iOS animation effects. The Uikit class typically has a animated: Parameter section, which allows you to use animations.
Core animation is also tightly coupled with quartz, each uiview is associated with a Calayer object, and Calayer is a layer in the core animation.
15.2 Core Animation Foundation
Core animation modifies the Calayer property when creating an animation, and then makes these properties smooth to change. Core Animation related knowledge Points:
Layers, where animations occur, Calayer is always associated with uiview and accessed through the Layer property.
Implicit animation, which is the simplest kind of animation, without setting a timer, regardless of thread or redraw.
Explicit animation, an animation created using Cabasicanimation, allows you to define more clearly how an attribute changes animation by Cabasicanimation.
Keyframe animations, a more complex type of explicit animation, where you can define the start and end of an animation, and you can define animations between certain frames.
15.3 Implicit animations
Example Ianimate:
#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface Ianimateviewcontroller: Uiviewcontroller { iboutlet uiimageview *plane;} -(Ibaction) Moveplane: (ID) sender; @end//--m-(Ibaction) Moveplane: (ID) Sender { [UIView beginanimations:nil Context:null]; Cgaffinetransform movetransform = Cgaffinetransformmaketranslation (n.); Plane.layer.affinetransform=movetransform; plane.layer.opacity = 1; [UIView commitanimations];}
The opacity of the airplane picture (opacity) is initially 0.25, and the opacity is set to 1.0 during the animation. This process is to set the plane image object's layer properties, plane.layer.opacity = 1;
[Plane.layer Setaffinetransform:movetransform]; Sets the affine movement transformation of the plane picture layer object.
15.4 Explicit Animations
Eanimate Key Code:
#import <UIKit/UIKit.h> #import <quartzcore/quartzcore.h>@ Interface Eanimateviewcontroller:uiviewcontroller {iboutlet uiimageview *plane;} -(Ibaction) Moveplane: (ID) sender; @end//--m-(Ibaction) Moveplane: (ID) Sender {cabasicanimation *opanim = [Cabasicanima tion animationwithkeypath:@ "opacity"]; opanim.duration = 3.0; Opanim.fromvalue = [NSNumber numberwithfloat:.25]; opanim.tovalue= [NSNumber numberwithfloat:1.0]; Opanim.cumulative = YES; Opanim.repeatcount = 2; [Plane.layer addanimation:opanim forkey:@ "animateopacity"]; Cgaffinetransform movetransform = cgaffinetransformmaketranslation (180, 200); Cabasicanimation *moveanim = [cabasicanimation animationwithkeypath:@ "transform"]; moveanim.duration = 6.0; moveanim.tovalue= [Nsvalue Valuewithcatransform3d:catransform3dmakeaffinetransform (MoveTransform)]; [Plane.layer addanimation:moveanim forkey:@ "Animatetransform"];}
Explicit animations do not have to define calayer changes and do not have to execute them, but instead define the animations individually by cabasicanimation. Each of these animations contains properties such as duration, repeatcount, and so on. Then, use the Addanimation:forkey: method to apply each animation to the layer, respectively.
[Cabasicanimation animationwithkeypath:@ "opacity"] gets the Transparency animation object, @ "transform" is the specified conversion animation.
The Opanim.cumulative property is the specified cumulative
Opanim.repeatcount Number of repeat executions
The Catransform3dmakeaffinetransform function is the matrix that transforms the affine transformation matrix into the Transform3D type used by the core animation.
15.5 Key-frame explicit animations
Eanimate_keyframe Key Code:
#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface Eanimateviewcontroller: Uiviewcontroller {iboutlet uiimageview *plane;} -(Ibaction) Moveplane: (ID) sender; @end//-m-(Ibaction) Moveplane: (ID) Sender {cakeyframeanimation *opanim = [Cakeyframe Animation animationwithkeypath:@ "opacity"]; opanim.duration = 6.0; Opanim.values =[nsarray arraywithobjects: [NSNumber numberwithfloat:0.25], [nsnum ber 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]; [Plane.layer addanimation:opanim forkey:@ "animateopacity"]; Cgaffinetransform movetransform = cgaffinetransformmaketranslation (180, 200); Cabasicanimation *moveanim = [cabasicanimation animationwithkeypath:@ "Transform"]; moveanim.duration = 6.0; moveanim.tovalue= [Nsvalue Valuewithcatransform3d:catransform3dmakeaffinetransform (MoveTransform)]; [Plane.layer addanimation:moveanim forkey:@ "Animatetransform"]; }
Animation.values is an array of values.
The animation.keytimes is a continuous time scale for each frame fragment, and the range of values 0.0-1.0 between.
Key frame Path Instance Ballsteps
#import <UIKit/UIKit.h> #import <QuartzCore/CoreAnimation.h> @interface Ballstepsviewcontroller: Uiviewcontroller {UIButton *drawbutton; Uiimageview *imageview;} @property (nonatomic, retain) iboutlet UIButton *drawbutton; @property (nonatomic, retain) iboutlet Uiimageview * imageview;-(ibaction) Drawstar: (ID) sender; @end//--m#import "BallStepsViewController.h" @implementation Ballstepsviewcontroller@synthesize DrawButton; @synthesize imageview;-(Ibaction) Drawstar: (ID) Sender {[DrawButton Setenabled:no]; Cgmutablepathref Starpath = cgpathcreatemutable (); Cgpathmovetopoint (starpath,null,160.0f, 100.0f); Cgpathaddlinetopoint (Starpath, NULL, 100.0f, 280.0f); Cgpathaddlinetopoint (Starpath, NULL, 260.0, 170.0); Cgpathaddlinetopoint (Starpath, NULL, 60.0, 170.0); Cgpathaddlinetopoint (Starpath, NULL, 220.0, 280.0); Cgpathclosesubpath (Starpath); Cakeyframeanimation *animation = nil; Animation = [Cakeyframeanimation animationwithkeypath:@ "position"]; [Animation setduration:10.0f]; [Animation setdelegate:self]; [Animation Setpath:starpath]; Cfrelease (Starpath); Starpath = nil; [[ImageView layer] addanimation:animation forkey:@ "position"];} -(void) Animationdidstop: (Caanimation *) Theanimation finished: (BOOL) flag {[DrawButton setenabled:yes];} -(void) Dealloc {[DrawButton release]; [ImageView release]; [Super Dealloc];} @end
[Cakeyframeanimation animationwithkeypath:@ "position"]; Creates a keyframe animation of the position type.
The path can be defined in the Keyframe animation, and the paths are placed in the cgmutablepathref similar to those in CG.
Cgpathclosesubpath (starpath); End path.
[Animation Setpath:starpath]; set the path.
[Animation setdelegate:self]; Sets the delegate object to itself, that is, the callback method animationdidstop:finished:.
Last Cfrelease (Starpath); release path.
15.6 UIView Level animations
In addition to using the core Animation layer to achieve animation, we also have uiview directly implement implicit animation.
Example Viewanimation:
@interface Mainviewcontroller:uiviewcontroller { uiimageview *animimageview; UIButton *button;} @property (assign) Iboutlet Uiimageview *animimageview; @property (assign) Iboutlet UIButton *button;-(ibaction) action :(ID) sender; @end
--m#import "MainViewController.h" @implementation mainviewcontroller@synthesize animimageview; @synthesize button; -(Ibaction) Action: (ID) Sender { [UIView beginanimations:@ "Hide button" Context:nil]; [[Self button] setalpha:0.0]; [UIView commitanimations]; [UIView beginanimations:@ "Slide Around" context:nil]; [UIView setanimationduration:1.0]; [UIView setanimationdelegate:self]; [UIView setanimationdidstopselector: @selector (viewanimationdone:)]; [UIView Setanimationrepeatcount:3]; [UIView Setanimationrepeatautoreverses:yes]; Cgpoint Center = [[Self animimageview] center]; Center.y + + +; [[Self Animimageview] setcenter:center]; [UIView commitanimations];} -(void) Viewanimationdone: (nsstring*) name { [UIView beginanimations:@ "Show button" Context:nil]; [[Self button] setalpha:1.0]; [UIView commitanimations];} @end
The animation in UIView is defined in the animation block, and the animation block is UIView beginanimations:context: Start, end at UIView commitanimations.
Start by setting the transparency of the button to 0, and the result is hidden when you start the animation.
Then, start the new animation by setting the delegate event:
[UIView Setanimationdelegate:self]
[UIView setanimationdidstopselector: @selector (viewanimationdone:)];
Call Viewanimationdone when the animation is finished: method.
Built-in UIView animations
UIView has a uiviewanimationtransition property that can be animated, and these animations are available in several popular animations for iOS:
Uiviewanimationtransitionnone
Uiviewanimationtransitionflipfromleft
Uiviewanimationtransitionflipfromright
Uiviewanimationtransitioncurlup
Uiviewanimationtransitioncurldown
Example Uiviewanimation
#import <UIKit/UIKit.h> @interface Uiviewanimationviewcontroller:uiviewcontroller {}-(ibaction) Douiviewanimation: (ID) sender; @end//-m#import "UIViewAnimationViewController.h" @implementation uiviewanimationviewcontroller-(ibaction) Douiviewanimation: (ID) sender{[UIView beginanimations:@ "AnimationID" Context:nil]; [UIView setanimationduration:1.5f]; [UIView Setanimationcurve:uiviewanimationcurveeaseinout]; [UIView Setanimationrepeatautoreverses:no]; UIButton *thebutton = (UIButton *) sender; Switch (thebutton.tag) {Case 1: [UIView setanimationtransition:uiviewanimationtransitionflipfromleft fo RView:self.view Cache:yes];//oglflip, Fromleft break; Case 2: [UIView setanimationtransition:uiviewanimationtransitionflipfromright ForView:self.view cache:yes];//og Lflip, Fromright break; Case 3: [UIView setanimationtransition:uiviewanimationtransitioncurlup ForView:self.view Cache:yes]; Break Case 4: [UIView setanimationtransition:uiviewanimationtransitioncurldown ForView:self.view Cache:yes]; Break Default:break; }//[self.view exchangesubviewatindex:1 withsubviewatindex:0]; [UIView commitanimations];} -(void) Viewdidunload {//Release any retained subviews of the main view. e.g. Self.myoutlet = nil;} -(void) Dealloc {[Super Dealloc];} @end
[UIView Setanimationcurve:uiviewanimationcurveeaseinout] Sets the animation curve, the animation curve specifies the way the animation enters and exits, it also has several constants:
Uiviewanimationcurveeaseinout
Uiviewanimationcurveeasein
Uiviewanimationcurveeaseout
Uiviewanimationcurvelinear
SetAnimationTransition:forView:cache: The first parameter of the method defines the animation type, the second parameter is the current View object, and the third parameter uses a buffer.
Animation of iOS