ios - 播放动画等待 2 秒并重复
问题描述
我UIView.animate
用来为一个简单的 UIView 制作动画。起始条件:(width = height = 30, color = blue
我在情节提要中设置)
我想将尺寸增加到 w=50,h=50 并使其变为白色,同时它具有适当的角半径。然后我重置视图并再次执行相同的动画。
这是我的代码:
class ViewController: UIViewController {
@IBOutlet weak var swipeRightView: UIView!
@IBOutlet weak var doubleTab: UIView!
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...2 {
startAnimation()
sleep(5)
self.doubleTab.layer.removeAllAnimations()
startAnimation()
}
}
func startAnimation() {
self.doubleTab.layer.cornerRadius = 15
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}) { (_) in
self.doubleTab.backgroundColor = UIColor(displayP3Red: 0.34, green: 0.61, blue: 0.88, alpha: 1)
self.doubleTab.frame.size = CGSize(width: 30, height: 30)
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}, completion: nil)
}
}
}
这很好用,但我想重复整个动画。整个动画完成后,我想等待 2 秒然后重复,然后等待 2 秒并重复,依此类推。正如您在中看到的,viewDidLoad
我尝试使用 a 重复动画,for-loop
但这没有影响或行为错误。Arepeat - while
也没有效果。那么,我怎样才能重复这个动画呢?非常感谢任何帮助。
解决方案
在这种情况下,您可以递归地startAnimation
调用该函数,这意味着在它完成后再次调用它,它会在完成后再次调用它自己,依此类推。
所以你基本上可以, completion: { _ in self.startAnimation() }
在你当前设置的最后一个块中添加completion: nil
.
如果要添加延迟,可以在函数中添加延迟作为参数,并0
在第一个动画周期中默认设置。
完整的代码将是:
func startAnimation(delay: TimeInterval = 0) {
self.doubleTab.layer.cornerRadius = 15
UIView.animate(withDuration: 1.5, delay: delay, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}) { (_) in
self.doubleTab.backgroundColor = UIColor(displayP3Red: 0.34, green: 0.61, blue: 0.88, alpha: 1)
self.doubleTab.frame.size = CGSize(width: 30, height: 30)
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}, completion: { _ in
self.startAnimation(delay: 2)
})
}
}
推荐阅读
- rust - Rust 链表不能借用上一个和下一个元素作为可变的(只需要不可变的引用)
- java - 引起:java.sql.SQLException:ORA-06550:第 1 行,第 7 列:PLS-00306:调用“PR_SP_FAHMI”时参数的数量或类型错误
- javascript - window.moveTo() 在 Firefox 中不会超过我屏幕的一半
- python-3.x - Python 3 中有异步抽象基类吗?
- ios - 如何在 StackView 中停止排列的子视图剪辑
- xamarin - 如何导航离开 MasterDetailPage 但不重置导航堆栈
- angular - 错误:AADSTS50058:已发送静默登录请求,但没有用户登录 - 移动 safari 浏览器中的 Angular ADAL 身份验证问题
- data-mining - ECLAT算法找到最大和闭合频繁集
- html - 响应式视频部分都具有 100vh 和 100vw 而不使用位置:固定
- rxjs - 如何完成combineLatest?