我可以强制元素立即完成它的动画吗?

时间:2016-12-26 17:07:32

标签: ios swift

这里我有一些代码来显示带标签的UIView作为通知。

self.not1cons.constant = 0
self.notificationLbl1.text = self.notification1

UIView.animate(withDuration: 2.5, delay: 0.3, options: .allowAnimatedContent, animations: {

self.view.layoutIfNeeded()

       }, completion: { finsihed in

            self.not1cons.constant = -100

            UIView.animate(withDuration: 2.5, delay: 2.0, options: .allowAnimatedContent, animations: {

                self.view.layoutIfNeeded()

       }, completion: { finshed in

           })
       })

它从屏幕外开始并下降到视图中。它会停留几秒钟并返回到屏幕外的原始位置。我需要一些代码来使这些链式动画立即发生。这可能吗?

1 个答案:

答案 0 :(得分:0)

你可以通过操纵系统在幕后生成的CAAnimations来实现这一点,但这是相当棘手的业务,并不是一个好主意,因为它依赖于当前实现的未记录的细节,这是有风险的。

相反,我建议您重新设计动画以使用新到iOS 10 UIViewPropertyAnimator,支持暂停和恢复动画,以及在任意点前后擦洗它们。

我在Gitub上有一个名为UIViewPropertyAnimator-test的演示项目,它允许您使用滑块来回擦洗动画。它比你的需要更复杂,但它应该给你这个想法。

相关问题