你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS-UIView動畫

iOS-UIView動畫

編輯:IOS開發綜合

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函數對包含動畫,都是通過參數設置動畫的執行屬性。

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved