UIViewPropertyAnimator在应用程序生命周期中做奇怪的事情 [英] UIViewPropertyAnimator does strange things in the application life cycle
问题描述
帮我弄清楚这种情况下发生的情况:
想象一下,您有一个具有以下属性的视图控制器
@IBOutlet弱var blurView:UIVisualEffectView!
var动画师:UIViewPropertyAnimator?
在 viewDidLoad()
中,您可以执行某些操作像这样
动画= UIViewPropertyAnimator(
持续时间:1,
曲线:.easeOut,
个动画:{
self.blurView.effect = UIBlurEffect(style:.light)
})
animator?.fractionComplete = 0
要查看工作中的模糊效果,您必须在其后放一些东西(例如UIImageView)。
您还可以使用UISlider来播放动画:
@IBAction私有函数sliderDidChange(_ sender:UISlider) {
animator?.fractionComplete = CGFloat(sender.value)
}
播放一段时间后,您将应用置于后台并返回。
您期望看到什么?
由于某种原因,动画在您进入背景时开始播放,而当您返回时,动画处于其最终状态!
但我注意到此行为与 UIVisualEffectView
有关。
如果您更改了动画制作器中的其他属性(例如 opacity
或 transform
),则动画制作器将起作用如我所料(当应用程序在后台运行时,什么都没有改变。)
我错过了一些东西还是UIKit的错误?
解决方案
只要应用程序恢复运行(例如 viewWillAppear
),将动画的当前状态设置为所需的值。
说明
正如您在问题中提到的,与相比,
。 UIVisualEffectView
的问题更多UIViewPropertyAnimator
要交互式地对 UIVisualEffectView
的模糊进行动画处理,一种替代解决方案是通过将其 speed
设置为 0
并修改该图层的 timeOffset
来暂停该图层的动画code>。我之所以这样说是因为它的副作用是,当应用程序从后台返回时,它没有还原为正确的 timeOffset
。我怀疑这里发生了类似的事情。
Help me to figure out what is happening in this scenario:
Immagine you have a view controller with the following properties
@IBOutlet weak var blurView: UIVisualEffectView!
var animator: UIViewPropertyAnimator?
In the viewDidLoad()
you do something like that
animator = UIViewPropertyAnimator(
duration: 1,
curve: .easeOut,
animations: {
self.blurView.effect = UIBlurEffect(style: .light)
})
animator?.fractionComplete = 0
To see the blur effect at work, you have to put something behind it (e.g. a UIImageView). You have also a UISlider to play with the animation:
@IBAction private func sliderDidChange(_ sender: UISlider) {
animator?.fractionComplete = CGFloat(sender.value)
}
After playing for a while you put the app in background and come back.
What do you expect to see?
For some reason the animation starts when you go in background, and when you come back the animation is in its final state!
But I noticed that this behavior is related to the UIVisualEffectView
.
If you change other properties in the animator (for example the opacity
or the transform
) the animator works as I expect (nothing changes when the app goes in background).
Did I miss something or is it a UIKit bug?
Solution
Whenever the application resumes (such as in viewWillAppear
), set the current state of your animation to your desired value.
Explanation
As you mentioned in your question, this is more of an issue with UIVisualEffectView
than it is with UIViewPropertyAnimator
.
To animate a UIVisualEffectView
's blur interactively, an alternative solution is to pause the layer's animation by settings its speed
to 0
and modifying the layer's timeOffset
. I mention this because it has the side effect of not being restored to the proper timeOffset
when the application returns from the background. I suspect something similar is occurring here.
这篇关于UIViewPropertyAnimator在应用程序生命周期中做奇怪的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!