天天看點

動畫---UIView動畫

iOS的動畫有很多種 其中UIView的動畫的動畫比較簡單

先介紹一下UIViewAnimationOptions

UIViewAnimationOptionLayoutSubviews //送出動畫的時候布局子控件,表示子控件将和父控件一同動畫。

UIViewAnimationOptionAllowUserInteraction //動畫時允許使用者交流,比如觸摸

UIViewAnimationOptionBeginFromCurrentState //從目前狀态開始動畫

UIViewAnimationOptionRepeat //動畫無限重複

UIViewAnimationOptionAutoreverse //執行動畫回路,前提是設定動畫無限重複

UIViewAnimationOptionOverrideInheritedDuration //忽略外層動畫嵌套的執行時間

UIViewAnimationOptionOverrideInheritedCurve //忽略外層動畫嵌套的時間變化曲線

UIViewAnimationOptionAllowAnimatedContent //通過改變屬性和重繪實作動畫效果,如果key沒有送出動畫将使用快照

UIViewAnimationOptionShowHideTransitionViews //用顯隐的方式替代添加移除圖層的動畫效果

UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套繼承的選項

//時間函數曲線相關

UIViewAnimationOptionCurveEaseInOut //時間曲線函數,由慢到快

UIViewAnimationOptionCurveEaseIn //時間曲線函數,由慢到特别快

UIViewAnimationOptionCurveEaseOut //時間曲線函數,由快到慢

UIViewAnimationOptionCurveLinear //時間曲線函數,勻速

//轉場動畫相關的

UIViewAnimationOptionTransitionNone //無轉場動畫

UIViewAnimationOptionTransitionFlipFromLeft //轉場從左翻轉

UIViewAnimationOptionTransitionFlipFromRight //轉場從右翻轉

UIViewAnimationOptionTransitionCurlUp //上卷轉場

UIViewAnimationOptionTransitionCurlDown //下卷轉場

UIViewAnimationOptionTransitionCrossDissolve //轉場交叉消失

UIViewAnimationOptionTransitionFlipFromTop //轉場從上翻轉

UIViewAnimationOptionTransitionFlipFromBottom //轉場從下翻轉

簡單動畫 animateWithDuration

一般用來做view的frame改變使動畫流暢

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

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations 

[UIView animateWithDuration:0.4 animations:^{
            self.animateview.center = CGPointMake(self.view.center.x + 10, self.view.center.y + 10);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                self.animateview.center = self.view.center;
            } completion:^(BOOL finished) {
                
            }];
        }];
           

animateWithDuration:delay:options:

這裡的option參數主要是用來指動畫view移動的快慢 

比如:UIViewAnimationOptionCurveLinear 是線性運動  而UIViewAnimationOptionCurveEaseIn 由到快

可以寫在一起比較一下

[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.animateview.center = CGPointMake(self.animateview.center.x, self.view.center.y);
    } completion:^(BOOL finished) {
        
    }];
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.xx.center = self.view.center;
    } completion:^(BOOL finished) {
        
    }];
           

transitionWithView

一般用來做view顯示隐藏效果  option是切換的效果 

[UIView transitionWithView:self.animateview duration:0.9 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.animateview.hidden = YES;
    } completion:^(BOOL finished) {
        
    }];
    [UIView transitionWithView:self.xx duration:0.9 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
        self.xx.hidden = YES;
    } completion:^(BOOL finished) {
        
    }];
           

transitionFromView

用于2個view之間的切換從fromView切換到toview

[UIView transitionFromView:self.topView toView:self.bottomView duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
      NSInteger topIndex = [self.view.subviews indexOfObject:self.topView];
      NSInteger bottomIndex = [self.view.subviews indexOfObject:self.bottomView];
      [self.view exchangeSubviewAtIndex:topIndex withSubviewAtIndex:bottomIndex];
  }];
           

usingSpringWithDamping: initialSpringVelocity:

damping指的是阻尼系數 這個系統在0,1之間 系數越大彈性越小 initialSpringVelocity 初始彈動速度 速度越快 停下的時候越緩慢因為時間已經定死了 自行對比幾組感覺一下

[UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:15 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.animateview.center = self.view.center;
    } completion:^(BOOL finished) {
        
    }];
           

animateKeyframesWithDuration

來自 iOS7 Day-by-Day :: Day 11 :: UIView Key-frame Animations

void(^animationBlock)() = ^{
        NSArray *rainbowColors = @[[UIColor orangeColor],
                                   [UIColor yellowColor],
                                   [UIColor greenColor],
                                   [UIColor blueColor],
                                   [UIColor purpleColor],
                                   [UIColor redColor]];
        
        NSUInteger colorCount = [rainbowColors count];
        for(NSUInteger i=0; i<colorCount; i++) {
            [UIView addKeyframeWithRelativeStartTime:i/(CGFloat)colorCount
                                    relativeDuration:1/(CGFloat)colorCount
                                          animations:^{
                                              self.xx.backgroundColor = rainbowColors[i];
                                          }];
    }
    };
        [UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModePaced animations:animationBlock completion:nil];
           

繼續閱讀