gpt4 book ai didi

ios - 将设置 UIView 属性显式停止/完成在同一属性上使用 UIViewPropertyAnimator 启动的现有动画

转载 作者:行者123 更新时间:2023-11-30 11:04:46 27 4
gpt4 key购买 nike

我目前的 iOS 开发知识水平还很陌生,处于学习阶段。提供此信息以便专家能够做出相应的回答:)

根据斯坦福大学的 iOS 开发类(class),据说如果另一个动画针对同一属性启动,则 UIView 属性的动画将被中断(更具体地说,新动画开始处理前一个动画的其中一个属性) .

链接到视频中教授此内容的确切位置 https://youtu.be/84ZhYhAwYqo?t=209

在 stackoverflow 上的一个问题的回答中也说了同样类似的话。直接链接回答https://stackoverflow.com/a/3076451/8503233

但是当我尝试这样做时,它并没有发生。

为了测试这一点,我制作了一个简单的应用程序,其中单击按钮开始使用 UIViewPropertyAnimator.runningPropertyAnimator 对 View 进行动画处理,并将 alpha 更改为 0。持续时间设置为 10 秒,以便我有足够的时间触发另一个动画。

然后另一个按钮将启动一个动画,将同一 View 的 alpha 更改为 1。我预计当第二个动画开始时,它将导致第一个动画停止并使用 UIViewAnimatingPosition 的值调用其完成处理程序作为.当前。

但我发现,即使第二个动画开始,第一个动画仍在运行,并且在运行完整的持续时间后,完成处理程序被调用,UIViewAnimatingPosition 为 .end。

这与我在上面给出的来源中读到的内容完全相反。请帮忙。这里究竟发生了什么。如果需要,将分享应用程序代码。谢谢!!!

编辑1:

查看 Controller 代码

class MyViewController: UIViewController {

@IBOutlet weak var viewToAnimate: UIView!
@IBOutlet weak var statusView: UIView!

@IBAction func buttonAnimate1(_ sender: UIButton) {
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 5, delay: 0, options: [], animations: {
self.viewToAnimate.alpha = 0.1
}, completion: {position in
switch position {
case .start:
print("In animation 1 completion block with position = start")
case .current:
print("In animation 1 completion block with position = current")
case .end:
print("In animation 1 completion block with position = end")
}
self.statusView.backgroundColor = UIColor.brown
})
}


@IBAction func buttonAnimate2(_ sender: UIButton) {
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 5, delay: 0, options: [.beginFromCurrentState], animations: {
self.viewToAnimate.alpha = 1
}, completion: {position in
switch position {
case .start:
print("In animation 2 completion block with position = start")
case .current:
print("In animation 2 completion block with position = current")
case .end:
print("In animation 2 completion block with position = end")
}
self.statusView.backgroundColor = UIColor.green
})
}


@IBAction func buttonRestore(_ sender: UIButton) {
viewToAnimate.alpha = 1
statusView.backgroundColor = UIColor.yellow
}
}

测试步骤:

在模拟器中,我按 Animate1 按钮,大约 1 秒后,我按 Animate2 按钮。

控制台输出:

In animation 1 completion block with position = end
In animation 2 completion block with position = end

在 @matt 的评论中,我们得知动画现在是可加的。这澄清了很多事情,但苹果的 addAnimations(_:delayFactor:) 文档说,我引用

If the animation block modifies a property that is being modified by a different property animator, then the animators combine their changes in the most appropriate way. For many properties, the changes from each animator are added together to yield a new intermediate value. If a property cannot be modified in this additive manner, the new animations take over as if the beginFromCurrentState option had been specified for a view-based animation.

因此,如果新动画隐式或显式地以 beginFromCurrentState 形式接管,那么当新动画接管时,第一个动画不应该以 .current 状态停止吗?

最佳答案

您正在查阅古代历史(您引用的答案来自 2010 年!)。 曾经确实,对于 View 动画,在已经进行动画处理的 View 属性上订购动画会突然取消现有动画。但在最新版本的 iOS 中, View 动画已经附加,这意味着默认情况下新动画将与现有的运行中动画平​​滑地组合。

关于ios - 将设置 UIView 属性显式停止/完成在同一属性上使用 UIViewPropertyAnimator 启动的现有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52847182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com