UIView動畫

一、基礎動畫:

(1)、Block方式:

    [UIView animateWithDuration:3.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        _imgView.center=location;
    } completion:^(BOOL finished) {
        NSLog(@"Animation.end");
    }];

(2)、靜態方式:

//方法2:靜態方法:
    [UIView beginAnimations:@"KCBasicAnimation" context:nil];
    [UIView setAnimationDuration:3.0];
    [UIView setAnimationDelay:1.0];//設置延遲
    [UIView setAnimationRepeatAutoreverses:NO];//是否回覆
    [UIView setAnimationRepeatCount:10];//重複次數
    [UIView setAnimationStartDate:(NSDate *)];//設置動畫開始運行的時間
    [UIView setAnimationDelegate:self];//設置代理
    [UIView setAnimationWillStartSelector:(SEL)];//設置動畫開始運動的執行方法
    [UIView setAnimationDidStopSelector:(SEL)];//設置動畫運行結束後的執行方法
    
    _imgView.center=location;
    
    //開始動畫
    [UIView commitAnimations];
(3)、新增彈簧效果動畫:

    [UIView animateWithDuration:5.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1.0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^{
        _imgView.center=location;
    } completion:nil];

(4)、options:

/*
    1.常規動畫屬性設置(可以同時選擇多個進行設置)
    
    UIViewAnimationOptionLayoutSubviews:動畫過程中保證子視圖跟隨運動。
    
    UIViewAnimationOptionAllowUserInteraction:動畫過程中允許用戶交互。
    
    UIViewAnimationOptionBeginFromCurrentState:所有視圖從當前狀態開始運行。
    
    UIViewAnimationOptionRepeat:重複運行動畫。
    
    UIViewAnimationOptionAutoreverse :動畫運行到結束點後仍然以動畫方式回到初始點。
    
    UIViewAnimationOptionOverrideInheritedDuration:忽略嵌套動畫時間設置。
    
    UIViewAnimationOptionOverrideInheritedCurve:忽略嵌套動畫速度設置。
    
    UIViewAnimationOptionAllowAnimatedContent:動畫過程中重繪視圖(注意僅僅適用於轉場動畫)。
    
    UIViewAnimationOptionShowHideTransitionViews:視圖切換時直接隱藏舊視圖、顯示新視圖,而不是將舊視圖從父視圖移除(僅僅適用於轉場動畫)
    UIViewAnimationOptionOverrideInheritedOptions :不繼承父動畫設置或動畫類型。
    
    2.動畫速度控制(可從其中選擇一個設置)
    
    UIViewAnimationOptionCurveEaseInOut:動畫先緩慢,然後逐漸加速。
    
    UIViewAnimationOptionCurveEaseIn :動畫逐漸變慢。
    
    UIViewAnimationOptionCurveEaseOut:動畫逐漸加速。
    
    UIViewAnimationOptionCurveLinear :動畫勻速執行,默認值。
    
    3.轉場類型(僅適用於轉場動畫設置,可以從中選擇一個進行設置,基本動畫、關鍵幀動畫不需要設置)
    
    UIViewAnimationOptionTransitionNone:沒有轉場動畫效果。
    
    UIViewAnimationOptionTransitionFlipFromLeft :從左側翻轉效果。
    
    UIViewAnimationOptionTransitionFlipFromRight:從右側翻轉效果。
    
    UIViewAnimationOptionTransitionCurlUp:向後翻頁的動畫過渡效果。
    
    UIViewAnimationOptionTransitionCurlDown :向前翻頁的動畫過渡效果。
    
    UIViewAnimationOptionTransitionCrossDissolve:舊視圖溶解消失顯示下一個新視圖的效果。
    
    UIViewAnimationOptionTransitionFlipFromTop :從上方翻轉效果。   
    
    UIViewAnimationOptionTransitionFlipFromBottom:從底部翻轉效果。
    */

二、關鍵幀動畫:
    //關鍵幀動畫
    [UIView  animateKeyframesWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionCurveLinear animations:^{
       [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
           _imgView.center=CGPointMake(80.0, 220.0);
       }];
       [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{
            _imgView.center=CGPointMake(45.0, 300.0);
       }];
        [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
            _imgView.center=CGPointMake(55.0, 400);
        }];
    } completion:^(BOOL finished) {
        NSLog(@"Animation end.");
    }];

三、轉場動畫:

    [UIView transitionWithView:_imgView duration:1.0 options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        _imgView.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    } completion:nil];
如果有兩個完全不同的視圖,並且每個視圖佈局都很複雜,此時要在這兩個視圖之間進行轉場可以使用+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0)方法進行兩個視圖間的轉場,需要注意的是默認情況下轉出的視圖會從父視圖移除,轉入後重新添加,可以通過UIViewAnimationOptionShowHideTransitionViews參數設置,設置此參數後轉出的視圖會隱藏(不會移除)轉入後再顯示。

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