gpt4 book ai didi

ios - 使用 UIViewPropertyAnimator 按顺序动画 UIView 的 alpha

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:41 24 4
gpt4 key购买 nike

我有一个 UIView,我想在 0.5 秒后显示,并在 0.5 秒后再次隐藏,创建一个简单的动画。我的代码如下:

    let animation = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) {
self.timerBackground.alpha = 1
let transition = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) {
self.timerBackground.alpha = 0
}
transition.startAnimation(afterDelay: 0.5)
}
animation.startAnimation()

当我测试它时,没有任何反应。我认为这是因为它们同时运行,这意味着它们相互抵消,但这不是“afterDelay”部分应该防止的吗?

如果我分别运行它们,即从隐藏淡化到可见,或者从可见淡化到隐藏,它会起作用,但是当我尝试按顺序运行它们时,它不起作用。

我的 UIView 不是不透明的或隐藏的。

最佳答案

您可以使用 Timer,并在每个计时器滴答上向您的 UIViewPropertyAnimator 对象添加出现/隐藏动画 block 。

这是一个代码库:

@IBOutlet weak var timerBackground: UIImageView!

private var timer: Timer?
private var isShown = false
private var viewAnimator = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear)

override func viewDidLoad() {
super.viewDidLoad()
viewAnimator.addAnimations {
self.timerBackground.alpha = 1
}
viewAnimator.startAnimation()
isShown = true

self.timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.startReversedAction), userInfo: nil, repeats: true)
}

func startReversedAction() {
// stop the previous animations block if it did not have time to finish its movement
viewAnimator.stopAnimation(true)
viewAnimator.addAnimations ({
self.timerBackground.alpha = self.isShown ? 0 : 1
})
viewAnimator.startAnimation()
isShown = !isShown
}

我已经为 dots jumping 实现了非常相似的行为的 iOS 10 Animations demo project .

请随时查看它以获取更多详细信息。

关于ios - 使用 UIViewPropertyAnimator 按顺序动画 UIView 的 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42971408/

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