[ios開發基礎知識之]界面常用基本動畫


本文來源:http://www.cnblogs.com/v2m_/archive/2011/10/28/2227979.html


一.基本方式:使用UIView類的UIViewAnimation擴展
函數說明

+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 開始準備動畫
+ (void)commitAnimations; // 運行動畫

// 沒有get方法,下面的set在快外調用無效
+ (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. YES的話,動畫(非最後一次)結束後動態復原到最開始狀態
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. 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; // 添加動畫到view上,cache是YES的時候比較高效,但是動畫過程中不能更新界面上的內容,NO時每一幀都重新畫,可以實時更新
+ (void)setAnimationsEnabled:(BOOL)enabled; // 是否忽略一些動畫設置
+ (BOOL)areAnimationsEnabled;

一個動畫的代碼

- (void)testAnimation{
	int index = 102;
	UIViewAnimationTransition transition = UIViewAnimationOptionTransitionNone;
    switch (index) {
        case 101:
            transition = UIViewAnimationTransitionCurlDown;
            break;
        case 102:
            transition = UIViewAnimationTransitionCurlUp;
            break;
        case 103:
            transition = UIViewAnimationTransitionFlipFromLeft;
            break;
        case 104:
            transition = UIViewAnimationTransitionFlipFromRight;
            break;
        default:
            break;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:transition forView:self.view cache:YES];
    
    // operation>>>
	//    [self.view bringSubviewToFront:mImage];
	//    [self.view sendSubviewToBack:mImage];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
	
    // end<<<<<<
    [UIView commitAnimations];	
}

其中transition取值範圍

typedef enum {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;

特點:基礎,使用方便,但是效果有限

二.block方式:使用UIView類的UIViewAnimationWithBlocks擴展

函數說明

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);//間隔,延遲,動畫參數(好像沒用?),界面更改塊,結束塊

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // toView added to fromView.superview, fromView removed from its superview界面替換,這裏的options參數有效

舉例:
[UIView animateWithDuration:0.7 delay:0 options:0 animations:^(void){
		self.view.alpha = 0.2;
		[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
		self.view.alpha = 1;
	} completion:^(BOOL finished)
	 {
		 
	 }];

當areAnimationsEnabled爲NO時,上面不能動畫顯示

{
        UIViewAnimationOptions options = UIViewAnimationOptionTransitionNone;
        switch (((UIButton*)sender).tag) {
            case 202:
                options = UIViewAnimationOptionTransitionCrossDissolve;
                break;
            case 203:
                options = UIViewAnimationOptionTransitionFlipFromTop;
                break;
            case 204:
                options = UIViewAnimationOptionTransitionFlipFromBottom;
                break;
            default:
                break;
        }

        [self.view bringSubviewToFront:lImage];
        [UIView transitionFromView:lImage toView:mImage duration:0.7 options:options completion:^(BOOL finished)
         {
             if (finished) {
                 [self.view addSubview:lImage];
                 [self.view sendSubviewToBack:lImage];
                 [self.view sendSubviewToBack:mImage];
             } 
         }];
    }

options取值範圍


enum {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing

UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,

UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,//ios5
};
typedef NSUInteger UIViewAnimationOptions;

特點:快捷方便,效果更多.可以如上示例1那樣實現界面個元素屬性漸進變化的動態展示

三.core方式:使用CATransition類
使用要引入QuartzCore.framework
官方有個示例:ViewTransitions
基本上就是


CATransition *transition = [CATransition animation];
transition.duration = 0.7;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;//{kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};

//更多私有{@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"};
transition.subtype = kCATransitionFromLeft;//{kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};

transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];

// 要做的
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

如果要修改NavigationController的push 和pop動畫 ,則直接按如下操作即可,這樣就修改了其默認的動畫效果。

 [self.navigationController.view.layer addAnimation:transition forKey:nil];



其中需要注意的是:界面調整(動畫前後展示的兩個界面)需要在動畫時間內完成,否則沒有效果,對下次也有影響,我測的不是太完整,大家自己寫個performSelector: withObject: afterDelay:試驗下看看就知道.

特點:動畫效果較多(但是私有),可以實現UIViewController之間的動畫

demo:animations.zip




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章