iOS-UIView動畫

來源:互聯網
上載者:User

iOS-UIView動畫

iOS-UIView動畫

 

今天的主題是UIView的動畫。

在iOS中UIView的動畫是基於CALayer動畫封裝。

動畫就是靜態圖片通過一定頻率顯示,給人們動畫的效果。

 

UIView動畫有基於類方法的實現和基於Block方法塊的實現。

 

一.UIView基於類方法的實現的使用

類方法列表:

 

@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;

 

該方法是動畫的起點,它總是和commitAnimation成對出現。

2.+ (void)commitAnimations

提交動畫,

其他的主要是用來設定動畫的代理,執行時間,順延強制動畫,是否自動的重複,重複的次數等。下面是它們的使用。

 

-(void)classMethodAnimation{    [UIView beginAnimations:@"animation" context:nil];    //設定動畫重複次數    [UIView setAnimationRepeatCount:10.];    //開始動畫改變它的位置從originPoint(0,0)變味originPoint(100,100)    anim.frame = CGRectMake(100, 100, 50, 50);        //設定動畫曲線,也就是動畫效果    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    //設定動畫延遲    [UIView setAnimationDelay:4];    //設定動畫代理    [UIView setAnimationDelegate:self];    //設定動畫停止時的時間    [UIView setAnimationDidStopSelector:@selector(animationStop)];    //設定動畫執行時間    [UIView setAnimationDuration:10];    //設定動畫是否自動反轉    [UIView setAnimationRepeatAutoreverses:YES];        //設定動畫開始時調用的方法    [UIView setAnimationWillStartSelector:@selector(startAnimation)];        [UIView commitAnimations];}

 

控制台運行輸出結果:(主要是動畫代理方法的調用,動畫效果見demo)

 

2015-04-06 08:28:14.230 ViewAnimation[1299:49180] 動畫停止了-[ViewController startAnimation]

2015-04-06 08:28:16.227 ViewAnimation[1299:49180] 動畫停止了-[ViewController animationStop]

 

二.UIView動畫block方法

 

@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

 

 

block的動畫效果就是把你想要改變的東西包含到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");    }];}


運行控制態輸出效果:

 

 

2015-04-06 08:33:49.944 ViewAnimation[1359:51632] block animation complet operation

兩個動畫小結:

通過block把動畫內容包含到block中實現動畫,使用beginAnimation和commitsAnimation函數對包含動畫,都是通過參數設定動畫的執行屬性。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.