iOS development UI chapter-Core animation (UIView package animation)
UIView Animation (first and last)
1. Brief description
Uikit integrates animations directly into the UIView class, and UIView provides animated support for these changes when some of the internal properties change
The work required to perform the animation is done automatically by the UIView class, but you still want to notify the view when you want to perform the animation, which requires that the code that changes the property be placed in [UIViewbeginanimations: nil context: nil] and Between [UIView commitanimations]
Common method Parsing:
+ (void)setanimationdelegate:(ID) delegate sets the animation proxy object, which sends a message to the proxy object when the animation starts or ends
+ (void)setanimationwillstartselector:(SEL) selector executes the selector of the delegate object when the animation is about to begin, and beginanimations: Context: Incoming parameters passed into the selector
+ (void)setanimationdidstopselector:(SEL) selector executes the selector of the delegate object when the animation finishes, and Beginanimations: Context: Incoming parameters passed into the selector
+ (void)setanimationduration:(nstimeinterval) duration of duration animation, in seconds
+ (void)setanimationdelay:(nstimeinterval) delay animation delay seconds before starting
+ (void)setanimationstartdate:(nsdate *) StartDate animation start time, default to now
+ (void)Setanimationcurve:(Uiviewanimationcurve) curve animation Rhythm Control
+ (void)setanimationrepeatcount:(float) repeatcount The number of repetitions of the animation
+ (void)setanimationrepeatautoreverses:(BOOL) repeatautoreverses if set to Yes, the effect of each repetition of the animation is followed by the opposite
+ (void)setanimationtransition:(uiviewanimationtransition) transition forview:(UIView *) View Cache:(BOOL) cache sets the transition effect for view views, transition Specifies the transition type, the cache setting yes represents the use of the view cache, performance is good
2. Code examples
1//2//YYVIEWCONTROLLER.M 3//01-uiview Package Animation 4//5//Created by Apple on 14-6-22. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet UIView * customview;13 @end16 @implementation YYViewController18-(void) VIEWDIDLOAD20 {[Super viewdidload];22 }24-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Event26 {27//Print animation block position at NSLog (@ "position before the animation is executed: %@ ", Nsstringfromcgpoint (Self.customView.center)); 29 30///end-to-end animation [UIView Beginanimations:nil context:nil];32 Perform animation 33//Set animation execution time [UIView setanimationduration:2.0];35//Set agent [UIView setanimationdelegate:self]; 37//Set the event for the animation to complete the invocation of [UIView setanimationdidstopselector: @selector (didstopanimation)];39 Self.customView.cente R=cgpointmake [UIView commitanimations];41}43-(void) DidStopAnimation45 {NSLog (@ "animation executed"); 47//plot the position of the animation block NSLog (@ "After the animation is executed:%@", Nsstringfromcgpoint (Self.customView.center));}50 @end
Execution Result:
Print the location of the animation block:
3.UIView-Encapsulated animations vs. Calayer animations
Animations can be animated using both UIView and Calayer, but in real-world development, it is mostly the use of UIView encapsulated animations, which are rarely used for calayer animations.
Calayer the difference between the core animation and the UIView animation:
UIView encapsulated animations do not bounce when they are finished. That is, if you change the position of the layer by Calayer the core animation, the surface is changed, but in fact its position is unchanged.
code example:
1//2//YYVIEWCONTROLLER.M 3//01-uiview Package Animation 4//5//Created by Apple on 14-6-22. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet UIView * customview;13 @end16 @implementation YYViewController18-(void) Touchesbegan: (Nsset *) touches withevent: ( Uievent *) event22 {23//1. Creating a core animation cabasicanimation *anima=[cabasicanimation animation];25//pan [EMAIL&NB Sp;protected] "position"; 27//Set animation for execution Anima.tovalue=[nsvalue Valuewithcgpoint:cgpointmake (200, 300)];29 30 Set the time to perform the animation anima.duration=2.0;32//Set animation does not delete animation after execution anima.removedoncompletion=no;34//Set up the latest state of Save animation 35 anima.fillmode=kcafillmodeforwards;36//ANIMA.FILLMODE=KCAFILLMODEBACKWARDS;37 38//Set Animation Agent-Anima.de LEGATE=SELF;40 41//2. Add Core animation [Self.customView.layer Addanimation:anima forkey:nil];43}44-(void) AnimatioNdidstart: (caanimation *) ANIM46 {47//The position of the plot animation block//NSLog (@ "position before the animation starts:%@", Nsstringfromcgpoint (self.customView.cent ER)); NSLog (@ "The position before the animation starts:%@", Nsstringfromcgpoint (self.customView.layer.position));}51-(void) Animationdidstop: (caanimation *) Anim finished: (BOOL) flag52 {53//Print animation block location of the NSLog (@ "position after execution of the animation:%@", NSSTRINGFROMCGP Oint (self.customView.layer.position))}56 @end
Printing results:
Second, block animation
1. Brief description
+ (void)animatewithduration:(nstimeinterval) Duration delay:(nstimeinterval) delay options :(uiviewanimationoptions) options animations:(void (^) (void)) animations completion:(void (^) (BOOL finished)) Completion
Parameter resolution:
Duration: Duration of animation
delay: Animation delays delay after seconds start
Options: Rhythm Control for animations
Animations: Place code that changes the properties of the view in this block
Completion: This block is automatically called after the animation is finished
Transition animations
+ (void)Transitionwithview:(UIView *) View duration:(nstimeinterval) duration Options:( uiviewanimationoptions) Options animations:(void (^) (void)) animations completion:(void (^) (BOOL Finished)) Completion
Parameter resolution:
Duration: Duration of animation
View: Views that require a transition animation
options: Types of transition animations
Animations: Place code that changes the properties of the view in this block
Completion: This block is automatically called after the animation is finished
+ (void)Transitionfromview:(UIView *) Fromview toview:(UIView *) Toview Duration:( Nstimeinterval) DurationOptions:(uiviewanimationoptions) options completion:(void (^) (BOOL Finished)) Completion
After the method call is complete, the following two lines of code are executed:
Add Toview to Parent view
[Fromview.superview Addsubview:toview];
Remove the Fromview from the parent view
[Fromview.superview Removefromsuperview];
Parameter resolution:
Duration: Duration of animation
options: Types of transition animations
Animations: Place code that changes the properties of the view in this block
Completion: This block is automatically called after the animation is finished
2. Code examples
1 #import "YYViewController.h" 2 3 @interface Yyviewcontroller () 4 @property (weak, nonatomic) Iboutlet UIView *custo MView; 5 6 @end 7 8 @implementation Yyviewcontroller 9-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event11 { //block code block animation [UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{ NSLog//Executed animation (@ "position before the animation starts:%@", Nsstringfromcgpoint (Self.customView.center)); Self.customview.center=cgpointmake (+), + } completion:^ (BOOL finished) { /////The first action after completion of the animation NSLog (@ "Animation execution complete"), NSLog (@ "position after execution of the animation:%@", Nsstringfromcgpoint (Self.customView.center));}];22}23 @ End
Printing results:
Tip: Self.customView.layer.position and self.customView.center are equivalent, because the default value for position is (0.5,0.5).
Third, supplementary
Frame animation for 1.UIImageView
Uiimageview allows a series of images to be displayed sequentially within a given time
Related attribute parsing:
animationimages: The picture to be displayed (a nsarray with UIImage)
animationduration: The time required to fully display all the pictures in the Animationimages
Animationrepeatcount: The number of times the animation was executed (default = 0, which represents an infinite loop)
Related method parsing:
-(void)startanimating; Start animation
-(void)stopanimating; Stop animation
-(BOOL)isanimating; Whether the animation is running
2.UIActivityIndicatorView
is a rotational progress wheel that can be used to inform the user that an operation is in progress and is normally initialized with Initwithactivityindicatorstyle.
Method parsing:
-(void)startanimating; Start animation
-(void)stopanimating; Stop animation
-(BOOL)isanimating; Whether the animation is running
The Uiactivityindicatorviewstyle has 3 values to choose from:
Uiactivityindicatorviewstylewhitelarge//Large white indicator
Uiactivityindicatorviewstylewhite//Standard size white indicator
Uiactivityindicatorviewstylegray//Grey indicator for white background
iOS development UI chapter-Core animation (UIView package animation)