萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS UIView 動畫詳解:從基礎 API 到實戰彈性交互

UIView 動畫系統是 iOS 交互體驗的核心。本文系統梳理塊語法、關鍵參數、彈性動畫、鏈式組合與性能優化。

基礎塊語法 (iOS 4+)

[UIView animateWithDuration:0.5 
                      delay:0 
                    options:UIViewAnimationOptionCurveEaseInOut 
                 animations:^{
    // 可動畫屬性:frame, bounds, center, transform, alpha, backgroundColor, contentOffset 等
    self.view.alpha = 0;
    self.button.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
    // 動畫完成回調
    [self.view removeFromSuperview];
}];

核心可動畫屬性

  • 幾何: frame、bounds、center、transform(旋轉/縮放/傾斜)
  • 外觀: alpha、backgroundColor、tintColor
  • 內容: contentOffset、contentInset(ScrollView)

⚠️ 不可動畫: layer.cornerRadius、layer.shadow*、font、text、bounds.size 直接賦值(需用 transform)。

動畫選項詳解

選項行為
UIViewAnimationOptionCurveEaseInOut慢-快-慢(默認)
UIViewAnimationOptionCurveLinear勻速
UIViewAnimationOptionCurveEaseIn慢開始
UIViewAnimationOptionCurveEaseOut慢結束
UIViewAnimationOptionRepeat循環
UIViewAnimationOptionAutoreverse往返(需配合 Repeat)
UIViewAnimationOptionAllowUserInteraction動畫期間允許用戶交互
UIViewAnimationOptionLayoutSubviews動畫中佈局子視圖
UIViewAnimationOptionBeginFromCurrentState從當前狀態開始(打斷前一動畫)

彈性動畫

[UIView animateWithDuration:0.6 
                      delay:0 
     usingSpringWithDamping:0.6 // 0~1,越小彈性越大
      initialSpringVelocity:0.8 // 初始速度
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
    self.cardView.center = targetCenter;
} completion:nil];

模擬物理彈簧效果,極大提升交互質感(如彈出菜單、卡片翻轉)。

鏈式/並行/序列動畫組合

// 序列:嵌套 completion
[UIView animateWithDuration:0.3 animations:^{
    self.view.alpha = 0;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 animations:^{
        self.otherView.alpha = 1;
    }];
}];

// 並行:同時啟動多個動畫塊
[UIView animateWithDuration:0.3 animations:^{
    self.view1.alpha = 0;
}];
[UIView animateWithDuration:0.3 animations:^{
    self.view2.transform = CGAffineTransformMakeTranslation(100, 0);
}];

// 關鍵幀動畫 (CAKeyframeAnimation) - 複雜軌跡
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.values = @[[NSValue valueWithCGPoint:start], [NSValue valueWithCGPoint:mid], [NSValue valueWithCGPoint:end]];
anim.keyTimes = @[@0, @0.5, @1];
anim.duration = 1.0;
[self.layer addAnimation:anim forKey:@"path"];

實戰案例:上下滑動面板切換

- (IBAction)togglePanel:(id)sender {
    CGFloat targetY = self.isExpanded ? self.collapsedY : self.expandedY;
    [UIView animateWithDuration:0.4 
                          delay:0 
         usingSpringWithDamping:0.8 
          initialSpringVelocity:0 
                        options:UIViewAnimationOptionAllowUserInteraction 
                     animations:^{
        self.panelView.center = CGPointMake(self.panelView.center.x, targetY);
        self.toggleButton.transform = self.isExpanded ? CGAffineTransformIdentity : CGAffineTransformMakeRotation(M_PI);
    } completion:^(BOOL finished) {
        self.isExpanded = !self.isExpanded;
    }];
}

性能優化原則

  • 啟用光柵化: view.layer.shouldRasterize = YES; view.layer.rasterizationScale = [UIScreen mainScreen].scale; 靜態複雜視圖動畫前開啟,動畫後關閉。
  • 避免佈局抖動:動畫中盡量不觸發 layoutSubviews,預先計算好終態 Frame/Transform。
  • 使用 Transform 代替 Frame: transform 不觸發佈局,GPU 直接合成,效能更高。
  • 圖層合成優化: view.layer.drawsAsynchronously = YES 針對繪制複雜視圖。

調試技巧

  • 模擬器 Command+T 啟用「慢動畫」模式。
  • Xcode Debug → View Debugging → Render Offscreen(檢測離屏渲染)。
  • Instruments → Core Animation → Color Hits Green / Misses Red / Offscreen Yellow。

動畫不只是「會動」,更要「動得順、動得準、動得省」。

copyright © 萬盛學電腦網 all rights reserved