核心動畫的接觸點滴(五)

1.簡單介紹

        UIKit直接將動畫集成到UIView類中,當內部的一些屬性發生改變時,UIView將爲這些改變提供動畫支持。動畫的執行過程全部由UIView類自動完成,我們只需要通過調用[UIView beginAnimations: nil context:nil]和[UIView commitAnimations]這2個方法來通知視圖即可。


常用的方法說明:


+ (void)setAnimationDelegate:(id)delegate   --------  設置動畫代理對象,當動畫開始或者結束時會發消息給代理對象


+ (void)setAnimationWillStartSelector:(SEL)selector   -------- 當動畫即將開始時,執行delegate對象的selector,並且把beginAnimations:context:中傳入的參數傳進selector


+ (void)setAnimationDidStopSelector:(SEL)selector  --------- 當動畫結束時,執行delegate對象的selector,並且把beginAnimations:context:中傳入的參數傳進selector


+ (void)setAnimationDuration:(NSTimeInterval)duration  --------- 動畫的持續時間,秒爲單位


+ (void)setAnimationDelay:(NSTimeInterval)delay  --------- 動畫延遲delay秒後再開始


+ (void)setAnimationStartDate:(NSDate *)startDate   -------- 動畫的開始時間,默認爲now


+ (void)setAnimationCurve:(UIViewAnimationCurve)curve  --------- 動畫的節奏控制


+ (void)setAnimationRepeatCount:(float)repeatCount  ---------- 動畫的重複次數


+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses  -------- 如果設置爲YES,代表動畫每次重複執行的效果會跟上一次相反


+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache  -------- 設置視圖view的過渡效果, transition指定過渡類型, cache設置YES代表使用視圖緩存,性能較好


代碼示例:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    

    //首尾式動畫

    [UIView beginAnimations:nil context:nil];

    //設置代理

    [UIView setAnimationDelegate:self];

    //設置執行需要的時間

    [UIView setAnimationDuration:1.0f];

    //設置延遲多久後執行

    [UIView setAnimationDelay:1.0f];

    //設置動畫重複次數

    [UIView setAnimationRepeatCount:MAXFLOAT];

    //設置動畫執行的節奏

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    //設置重複執行時是否跟之前相反(YES代表相反)

    [UIView setAnimationRepeatAutoreverses:YES];

    //設置動畫將要執行時的回調

    [UIView setAnimationWillStartSelector:@selector(willStartAnimation)];

    //設置動畫完成後的回調

    [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];

    _customView.center = CGPointMake(Width/2+ViewWidth, Height/2);


    [UIView commitAnimations];


}


-(void)willStartAnimation

{

    

    NSLog(@"動畫將要執行時動畫塊的位置:%@",NSStringFromCGPoint(_customView.center));


}


-(void)didStopAnimation

{


    NSLog(@"動畫已經結束後動畫塊的位置:%@",NSStringFromCGPoint(_customView.center));


}


提示:

UIView和CALayer都可以實現視圖動畫,但一般在實際項目中還是使用UIView來實現動畫,因爲它在執行完動畫後不回反彈。CALayer在表面上改變了layer的位置狀態,實際上是沒有做更改。不信可以用之前博文裏的代碼測試一下就知道了~


使用block回調的動畫:

1.常用動畫

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion


參數解析:

duration:      動畫執行的時間

delay:           動畫延遲多久後開始

options:        動畫的節奏類型

animations: 將改變視圖屬性的代碼放在這個block中

completion:動畫結束後,會自動調用這個block



2.轉場動畫(一)

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion


參數解析:

duration:      動畫的持續時間

view:             需要進行轉場動畫的視圖

options:        轉場動畫的類型

animations: 將改變視圖屬性的代碼放在這個block中

completion動畫結束後,會自動調用這個block

 轉場動畫(二)

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion


參數解析:

duration:動畫的持續時間

options:轉場動畫的類型

animations:將改變視圖屬性的代碼放在這個block中

completion:動畫結束後,會自動調用這個block


調用這個方法相當於執行了下面兩句代碼:

// 添加toView到父視圖

[fromView.superview addSubview:toView]; 

// 把fromView從父視圖中移除

[fromView  removeFromSuperview];


代碼示例:

[UIView transitionWithView:self.customView duration:3.0 options:0 animations:^{             //執行的動畫

        NSLog(@"動畫開始執行前的位置:%@",NSStringFromCGPoint(self.customView.center));

        self.customView.center=CGPointMake(200, 300);

    } completion:^(BOOL finished) {

        //動畫執行完畢後的首位操作

        NSLog(@"動畫執行完畢");

        NSLog(@"動畫執行完畢後的位置:%@",NSStringFromCGPoint(  self.customView.center));

    }];


注意:self.customView.layer.position和self.customView.center等價,因爲position的默認值爲(0.5,0.5)。


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