萬盛學電腦網

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

iOS 4+ UIView 動畫塊回調:從舊式 Delegate 遷移到 Block-based API

舊式 API (iOS 4 前,已棄用)


[UIView beginAnimations:@"ViewRemove" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
myView.center = CGPointMake(myView.center.x, myView.center.y + 480);
[UIView commitAnimations];

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if ([animationID isEqualToString:@"ViewRemove"]) {
        [myView removeFromSuperview];
    }
}

缺點:字符串標識易出錯、上下文傳遞麻煩、無法捕獲局部變量、代碼分散。

現代 Block-based API (iOS 4+ 推薦)


[UIView animateWithDuration:0.3
                      delay:0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
    myView.center = CGPointMake(myView.center.x, myView.center.y + 480);
}
                 completion:^(BOOL finished) {
    if (finished) {
        [myView removeFromSuperview];
    }
}];

優勢:

  • 代碼內聚:動畫與完成邏輯在一起
  • 捕獲局部變量:直接使用 myView、self 等
  • 類型安全:finished 為 BOOL,無需字符串比對
  • 鏈式動畫:completion 中再次調用 animateWithDuration 實現序列

完整參數詳解


+ (void)animateWithDuration:(NSTimeInterval)duration
                     delay:(NSTimeInterval)delay
                   options:(UIViewAnimationOptions)options
                animations:(void (^)(void))animations
                completion:(void (^)(BOOL finished))completion;
參數說明
duration動畫時長(秒)
delay延遲啟動時間
options曲線/重複/自動反轉/佈局等組合:
UIViewAnimationOptionCurveLinear/EaseIn/EaseOut/EaseInOut
UIViewAnimationOptionRepeat/Autoreverse
UIViewAnimationOptionAllowUserInteraction
UIViewAnimationOptionLayoutSubviews
animations可動畫屬性:frame/bounds/center/transform/alpha/backgroundColor 等
completion動畫結束回調,finished=YES 表示正常完成,NO 表示被打斷

進階:彈簧動畫


[UIView animateWithDuration:0.5
                      delay:0
     usingSpringWithDamping:0.6
      initialSpringVelocity:0.8
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
    view.transform = CGAffineTransformMakeScale(1.2, 1.2);
}
                 completion:nil];

鏈式/並行/嵌套動畫


// 序列
[UIView animateWithDuration:0.3 animations:^{
    view.alpha = 0;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 animations:^{
        view.transform = CGAffineTransformMakeTranslation(0, -100);
    }];
}];

// 並行
[UIView animateWithDuration:0.3 animations:^{
    view.alpha = 0;
    view.transform = CGAffineTransformMakeScale(0.5, 0.5);
}];

// 嵌套 (iOS 8+)
[UIView animateWithDuration:0.3 animations:^{
    view.alpha = 0;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.2 animations:^{
        innerView.alpha = 0;
    }];
}];

遷移檢查清單

  • ✅ 所有 beginAnimations/commitAnimations 替換為 animateWithDuration:...
  • ✅ 移除 setAnimationDelegate: / setAnimationDidStopSelector:
  • ✅ 字符串 ID 改為閉包捕獲
  • ✅ context void* 改為閉包捕獲對象
  • ✅ 檢查 finished 參數處理中斷情況
copyright © 萬盛學電腦網 all rights reserved