iOS development UI chapter-Core animation (UIView package animation)

Source: Internet
Author: User

Original http://www.cnblogs.com/wendingding/p/3802830.html

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 [UIView beginanimations : 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.m3//01-uiview Package Animation4//5//Created by Apple on 14-6-22.6//Copyright (c) 2014 itcase. All rights reserved.7//87 mImport"YYViewController.h "10@interface Yyviewcontroller ()12@property (Weak, nonatomic) Iboutlet UIView *customview;1314@end16@implementation Yyviewcontroller1819-(void) Viewdidload20 {21 [Super Viewdidload];2223}2425-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event26 {27//Print the location of the animation blockNSLog (@"Position before Animation:%@ ", Nsstringfromcgpoint (Self.customView.center));2930//End-to-end animations[UIView Beginanimations:nil Context:nil];32//Performing animations33//Set animation Execution Time[UIView setanimationduration:2.0];35//Set up Proxy[UIView setanimationdelegate:self];37// Sets the event that the animation is executed to complete the call 38 [UIView setanimationdidstopselector:  @selector (didstopanimation)]; 39 Self.customview.center=cgpointmake (200, 300); Span class= "number" >40 [UIView commitanimations]; 41 42}43 44-( void) didstopanimation45 {46 NSLog (@  " Animation execution complete"); 47 // print animation block position 48 NSLog (@< Span class= "string" > " After the execution of the animation position:%@", Nsstringfromcgpoint (Self.customView.center)); 49}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.m3//01-uiview Package Animation4//5//Created by Apple on 14-6-22.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"1011 @Interface Yyviewcontroller ()@property (Weak, nonatomic) Iboutlet UIView *customview;1314@end16@implementation Yyviewcontroller18192021-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event22 {23//1. Create a core animationCabasicanimation *anima=[cabasicanimation Animation];25//TranslationAnima.keypath=@"Position ";27//Animating the executionAnima.tovalue=[nsvalue Valuewithcgpoint:cgpointmake (200,300)];2930//Set the time to perform the animationanima.duration=2.0;32//Sets the animation to not delete after execution finishesAnima.removedoncompletion=no;34//Set up the latest state for saving animationsAnima.fillmode=kcafillmodeforwards;36//Anima.fillmode=kcafillmodebackwards;3738//Set up an animated proxyAnima.delegate=self;4041//2. Add a core animation[Self.customView.layer Addanimation:anima Forkey:nil];43}4445-(void) Animationdidstart: (caanimation *) Anim46 {  The position of the print animation block/ /  NSLog (@ "position before the animation starts:%@", Nsstringfromcgpoint (Self.customView.center));  NSLog (@ "The position before the animation starts:%@", Nsstringfromcgpoint (self.customView.layer.position));  (void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag   Print the location of the animation block NSLog (@ " after the animation is executed:%@", Nsstringfromcgpoint (self.customView.layer.position));   @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) Duration Options :(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"23 @Interface Yyviewcontroller ()4 @property (weak, nonatomic) Iboutlet UIView *customview;56 @end78 @implementation Yyviewcontroller910-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event11 {12//Block code blocks Animation[UIView transitionWithView:self.customView Duration:3.0 Options:0 Animations:^{14 // animated 15 NSLog (@ " The position before the animation starts:%@ ", Nsstringfromcgpoint (Self.customView.center)); 16 Self.customview.center=cgpointmake (200, 300); Span class= "number" >17} completion:^ (BOOL finished) {18 // First action after completion of animation 19 NSLog (@"  Animation execution completed "); 20 NSLog (@ "21}]; 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).

iOS development UI chapter-Core animation (UIView package animation)

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.