Xcode 14.2 升级一个 Swift 项目时有编译警告️⚠提示 beginAnimations()iOS 13.0 后已经不推荐使用,应该用 block-based animation 代替。

1
2
3
4
5
6
7
UIView.beginAnimations(nil, context: nil) // 'beginAnimations(_:context:)' was deprecated in iOS 13.0: Use the block-based animation API instead
UIView.setAnimationDuration(0.5) // 'setAnimationDuration' was deprecated in iOS 13.0: Use the block-based animation API instead
UIView.commitAnimations() // 'commitAnimations()' was deprecated in iOS 13.0: Use the block-based animation API instead 

currentOverlay = overlay
currentOverlayTarget = overlayTarget
currentLoadingText = loadingText

修改为使用 Block-based animation 的代码:

1
2
3
4
5
UIView.animate(withDuration: 0.5) {
    currentOverlay = overlay
    currentOverlayTarget = overlayTarget
    currentLoadingText = loadingText
}

如果用 Objective-C 则修改如下:

1
2
3
4
5
6
[UIView animateWithDuration: 0.5
                 animations: ^{
    currentOverlay = overlay;
    currentOverlayTarget = overlayTarget;
    currentLoadingText = loadingText;
}];