首页 > 解决方案 > Swift——删除 UIView(动画)时,它会留在屏幕上

问题描述

我在 Swift 中有以下代码。问题是当它被执行时,它执行动画,但窗口保持在其原始位置,如我附加的 gif 所示。

UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModePaced, animations: {
    self.view.viewWithTag(123)?.frame = CGRect(x: screenWidth5/2, y: screenHeight, width: screenWidth5 * 4, height: screenHeight5 * 3 )
}, completion: .none)

self.view.viewWithTag(123)?.removeFromSuperview()

感谢您的时间和回答。

标签: iosswiftuiviewuiviewanimationuiviewanimationtransition

解决方案


您需要放入removeFromSuperview完成处理程序。

UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModePaced, animations: {
    self.view.viewWithTag(123)?.frame = CGRect(x: screenWidth5/2, y: screenHeight, width: screenWidth5 * 4, height: screenHeight5 * 3 )
}) { _ in
    self.view.viewWithTag(123)?.removeFromSuperview()
}            

推荐阅读