UIView encapsulation animation and uiview Encapsulation

Source: Internet
Author: User

UIView encapsulation animation and uiview Encapsulation

I. UIView animation (first and last animation)

1. Simple Description

UIKit directly integrates the animation into the UIView class. When some internal attributes change, UIView provides animation support for these changes.

The work required to execute the animation is automatically completed by the UIView class, but you still need to notify the view when you want to execute the animation. Therefore, you need to put the code for changing the attribute in [UIViewBeginAnimations: NilContext: Nil] and [UIViewCommitAnimations]

Common Methods:

+ (Void)SetAnimationDelegate(Id) delegate sets the animation proxy object. When the animation starts or ends, a message is sent to the proxy object.

+ (Void)SetAnimationWillStartSelector:( SEL) selector: When the animation is about to begin, execute the selector of the delegate object and pass the parameters passed in beginAnimations: context: To the selector.

+ (Void)SetAnimationDidStopSelector:( SEL) selector: When the animation ends, execute the selector of the delegate object and pass the parameters passed in beginAnimations: context: To the selector.

+ (Void)SetAnimationDuration:( NSTimeInterval) duration of the duration animation, in seconds

+ (Void)SetAnimationDelay:( NSTimeInterval) delay animation starts after delay seconds

+ (Void)SetAnimationStartDate:( NSDate *) Start Time of the startDate animation. The default value is now.

+ (Void)SetAnimationCurve:( UIViewAnimationCurve) rhythm control of curve Animation

+ (Void)SetAnimationRepeatCount:( Float) Number of repeatCount animation repetitions

+ (Void)SetAnimationRepeatAutoreverses(BOOL) If repeatAutoreverses is set to YES, it means that the effect of repeated animation execution will be different from the previous one.

+ (Void)SetAnimationTransition:( UIViewAnimationTransition) transitionForView:( UIView *) viewCache(BOOL) cache sets the transition effect of the view. transition specifies the transition type. The cache setting YES indicates that the view cache is used, and the performance is good.

2. Sample Code

 9 #import "YYViewController.h"10 11 @interface YYViewController ()12 @property (weak, nonatomic) IBOutlet UIView *customView;13 15 @end16 17 @implementation YYViewController18 19 - (void)viewDidLoad20 {21     [super viewDidLoad];    23 }
25-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event26 {27 // print the position of the animation Block 28 NSLog (@ "position before animation execution: % @ ", NSStringFromCGPoint (self. customView. center); 29 30 // animation 31 [UIView beginAnimations: nil context: nil]; 32 // execute animation 33 // set animation execution time 34 [UIView setAnimationDuration: 2.0]; 35 // set proxy 36 [UIView setAnimationDelegate: self]; 37 // set the event 38 for calling after the animation is executed [UIView setAnimationDidStopSelector: @ selector (didStopAnimation)]; 39 self. customView. center = CGPointMake (200,300); 40 [UIView commitAnimations]; 41 42} 43 44-(void) didStopAnimation45 {46 NSLog (@ "animation completed "); 47 // print the position of the animation block 48 NSLog (@ "position after animation execution: % @", NSStringFromCGPoint (self. customView. center); 49} 50 @ end

Execution result:

Position of the animation block:

3. Comparison between the animations encapsulated by UIView and CALayer animations

Both UIView and CALayer can be used to achieve the animation effect. However, in actual development, the animation encapsulated by UIView is generally used, but the CALayer animation is rarely used.

Difference between UIView and core Animation

1. CALayer core animation only works on layer.

2. The core animation of CALayer is a fake image. It does not modify the real position of the UIView.

When to use core Animation

1. When you do not need to interact with users, use the core animation.

2. Use the core animation when you want to make an animation based on the path.

3. Use the core animation when it is used as an animation (there are many types of core animation)

Ii. block Animation

1. Simple Description

Basic Animation

+ (Void)AnimateWithDuration:( NSTimeInterval) durationDelay:( NSTimeInterval) delayOptions:( UIViewAnimationOptions) optionsAnimations:( Void (^) (void) animationsCompletion:( Void (^) (BOOL finished) completion

Parameter Parsing:

Duration: Animation duration

Delay: Animation starts after delay seconds.

Options: Animation rhythm Control

Animations: Place the code for changing the view attribute in this block.

Completion: The block is automatically called after the animation ends.

Transfer Animation

+ (Void)TransitionWithView:( UIView *) viewDuration:( NSTimeInterval) durationOptions:( UIViewAnimationOptions) optionsAnimations:( Void (^) (void) animationsCompletion:( Void (^) (BOOL finished) completion

Parameter Parsing:

Duration: Animation duration

View: View for transition Animation

Options: Transfer animation type

Animations: Place the code for changing the view attribute in this block.

Completion: The block is automatically called after the animation ends.

+ (Void)TransitionFromView:( UIView *) fromViewToView:( UIView *) toViewDuration:( NSTimeInterval) durationOptions:( UIViewAnimationOptions) optionsCompletion:( Void (^) (BOOL finished) completion

After the method is called, the following code is executed:

// Add toView to parent View

[FromView. superview addSubview: toView];

// Remove fromView from parent View

[FromView removeFromSuperview];

Parameter Parsing:

Duration: Animation duration

Options: Transfer animation type

Animations: Place the code for changing the view attribute in this block.

Completion: The block is automatically called after the animation ends.

2. Sample Code

1 # import "YYViewController. h "2 3 @ interface YYViewController () 4 @ property (weak, nonatomic) IBOutlet UIView * customView; 5 6 @ end 7 8 @ implementation YYViewController 9 10-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event11 {12 // block code block animation 13 [UIView transitionWithView: self. customView duration: 3.0 options: 0 animations: ^ {14 // animation 15 NSLog (@ "position before animation execution: % @", NSStringFromCGPoint (self. customView. center); 16 self. customView. center = CGPointMake (200,300); 17} completion: ^ (BOOL finished) {18 // The first operation 19 NSLog (@ "animation completed") after the animation is executed "); 20 NSLog (@ "position after animation execution: % @", NSStringFromCGPoint (self. customView. center); 21}]; 22} 23 @ end

Print result:

Tip: self. customView. layer. position is equivalent to self. customView. center, because the default value of position is (0.5, 0.5 ).

Iii. Supplement

1. Frame Animation of UIImageView

UIImageView allows a series of images to be displayed in sequence within a specific period of time.

Resolution of related attributes:

AnimationImages: The image to be displayed (an NSArray with UIImage)

AnimationDuration: The time required to display all images in an animation.

AnimationRepeatCount: Number of animation executions (0 by default, indicating an infinite loop)

Analysis of related methods:

-(Void)StartAnimating; Start Animation

-(Void)StopAnimating; Stop Animation

-(BOOL)IsAnimating; Whether the animation is running

2. UIActivityIndicatorView

It is a rotation progress wheel, which can be used to inform the user that an operation is in progress. It is generally initialized using initWithActivityIndicatorStyle.

Method Analysis:

-(Void)StartAnimating; Start Animation

-(Void)StopAnimating; Stop Animation

-(BOOL)IsAnimating; Whether the animation is running

UIActivityIndicatorViewStyle has three values available:

UIActivityIndicatorViewStyleWhiteLarge // large white indicator

UIActivityIndicatorViewStyleWhite // standard white indicator

UIActivityIndicatorViewStyleGray // gray indicator for white background

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.