首页 > 解决方案 > 同时 UIViewController 过渡动画

问题描述

我正在尝试淡入背景渐变图像并将uiview卡片从底部(屏幕外)向上滑动到中心- 当模态呈现uiviewcontroller时同时执行两个动画。uiviewcontroller

我尝试将uiviewcontroller模态过渡样式设置为交叉溶解,这将为背景渐变图像提供淡入效果,并在运行动画时将卡片从底部viewDidAppear向上滑动到中心。uiview

虽然这可行,但卡有一点延迟,理想情况下我希望两个动画同时发生。

这种分组可以吗?任何指导将不胜感激。

以下是模态呈现的视图控制器中的相关代码:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    alertViewCenterYConstraint.constant += view.bounds.height

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseOut, animations: {
        self.alertViewCenterYConstraint.constant = 0
        self.view.layoutIfNeeded()
    }, completion: nil)
}

标签: iosswiftuiviewanimationpresentmodalviewcontroller

解决方案


这个工作就像一个魔术。我喜欢它。

   override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    alertViewCenterYConstraint.constant += view.bounds.height
    DispatchQueue.main.async {
    UIView.animate(withDuration: 0.1, delay: 0.05, options: .curveEaseOut, animations: {
        self.alertViewCenterYConstraint.constant = 0
        self.view.layoutIfNeeded()
    }, completion: nil)

    }
}

推荐阅读