gpt4 book ai didi

ios - 如何在 iOS 中为不断闪烁的灯光制作动画?

转载 作者:行者123 更新时间:2023-11-28 13:44:49 25 4
gpt4 key购买 nike

我正在尝试构建一个带有 NEON 的 iOS 应用程序,它会像真实的 NEON 一样随机且持续闪烁。

我不太确定如何让动画不断重复,我也不确定如何让它自动且永远运行。我把它放在 viewDidLoad 中,但我不确定那是否真的是放置它的最佳位置?

    UIImageView.animate(withDuration: 0.05, delay: 5.0, options: .repeat, animations: {
UIImageView.animate(withDuration: 0.05, delay: 2.0, animations: {
self.Aletter.alpha = 0.2
}) { (_) in
UIImageView.animate(withDuration: 0.05, delay: 0.0, animations: {
self.Aletter.alpha = 1.0
}, completion: { (_) in
UIImageView.animate(withDuration: 0.05, delay: 2.0, animations: {
self.Aletter.alpha = 0.6
}, completion: { (_) in
UIImageView.animate(withDuration: 0.05, delay: 0.0, animations: {
self.Aletter.alpha = 1.0
}, completion: { (_) in
UIImageView.animate(withDuration: 0.05, delay: 0.0, animations: {
self.Aletter.alpha = 0.6
}, completion: { (_) in
UIImageView.animate(withDuration: 0.05, delay: 0.0, animations: {
self.Aletter.alpha = 1.0
}, completion: { (_) in
UIImageView.animate(withDuration: 0.05, delay: 0.0, animations: {
self.Aletter.alpha = 0.6
}, completion: { (_) in
UIImageView.animate(withDuration: 0.05, delay: 0.0, animations: {
self.Aletter.alpha = 1.0
}, completion: { (_) in

})
})
})
})
})
})
})
}
})

此代码将运行我的一系列闪烁,但只会运行一次。我需要它持续运行。

最佳答案

正如 Matt 在评论部分所建议的那样,您可以使用如下内容:

private func flicker() { [weak self] in
UIView.animate(withDuration: 0.05, animations: {
self?.Aletter.alpha = CGFloat.random(in: 0.1...1.0)
}) { _ in
// When this round of animations completes call the same method again to start the animations again with a new random value for alpha.
self?.flicker()
}
}

只需在 viewDidLoad() 中调用 flicker()flicker 方法为 Aletter View 启动一个具有随机 alpha 值的动画,当此动画完成时它会再次调用自身。

作为旁注,请为您的变量使用小写首字母,Aletter 应该是 aletteraLetter,具体取决于上下文。

关于ios - 如何在 iOS 中为不断闪烁的灯光制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55543723/

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