IOS-UIView Animation
IOS-UIView Animation
Today's topic is the animation of UIView.
In iOS, The UIView animation is encapsulated based on the CALayer animation.
Animation is a static picture that is displayed at a certain frequency to give people the animation effect.
UIView animations are implemented based on class methods and Block-Based Method blocks.
I. Use of UIView class-based methods
Class Method list:
@interface UIView(UIViewAnimation)+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested+ (void)commitAnimations; // starts up any animations when the top level animation is commited// no getters. if called outside animation block, these setters have no effect.+ (void)setAnimationDelegate:(id)delegate; // default = nil+ (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context+ (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // current limitation - only one per begin/commit block+ (void)setAnimationsEnabled:(BOOL)enabled; // ignore any attribute changes while set.+ (BOOL)areAnimationsEnabled;+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);@end
1. + (void) beginAnimations :( NSString *) animationID context :( void *) context;
This method is the starting point of the animation. It always appears in pairs with commitAnimation.
2. + (void) commitAnimations
Submit an animation,
Others are mainly used to set the animation proxy, execution time, delayed animation execution, automatic repetition, and repeated times. Below are their usage.
-(Void) classMethodAnimation {[UIView beginAnimations: @ "animation" context: nil]; // set the number of animation repetitions [UIView setAnimationRepeatCount: 10.]; // start the animation and change its position from originPoint (0, 0) to originPoint (100,100) anim. frame = CGRectMake (100,100, 50, 50); // set the animation curve, that is, the animation effect [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; // set the animation delay [UIView setAnimationDelay: 4]; // set the animation proxy [UIView setAnimationDelegate: self]; // set the animation stop time [UIView setAnimationDidStopSelector: @ selector (animationStop)]; // set the animation execution time [UIView setAnimationDuration: 10]; // sets whether the animation is automatically reversed [UIView setAnimationRepeatAutoreverses: YES]; // sets the method called at the animation start [UIView setAnimationWillStartSelector: @ selector (startAnimation)]; [UIView commitAnimations];}
Console running output result: (mainly calling the animation proxy method. For the animation effect, see the demo)
08:28:14. 230 ViewAnimation [1299: 49180]Animation stopped-[ViewController startAnimation]
08:28:16. 227 ViewAnimation [1299: 49180]Animation stopped-[ViewController animationStop]
Ii. UIView animation block Method
@interface UIView(UIViewAnimationWithBlocks)+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views options:(UIViewAnimationOptions)options animations:(void (^)(void))parallelAnimations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);@end
The animation effect of block is to include the things you want to change into the block animation block.
-(void)blockAnimation{ [UIView animateWithDuration:5 animations:^{ anim.frame = CGRectMake(100, 100, 50, 50); }]; [UIView animateWithDuration:5 animations:^{ anim.frame = CGRectMake(200, 200, 50, 50); anim.backgroundColor = [UIColor grayColor]; } completion:^(BOOL finished) { NSLog(@"block animation complet operation"); }];}
Output results of the Running Control Mode:
08:33:49. 944 ViewAnimation [1359: 51632] block animation complet operation
Two animation summary:
You can use a block to include the animation content in a block to implement an animation. You can use the beginAnimation and commitsAnimation functions to set the animation execution attributes through parameters.