首页 > 解决方案 > 播放动画等待 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也没有效果。那么,我怎样才能重复这个动画呢?非常感谢任何帮助。

标签: iosswiftuiview

解决方案


在这种情况下,您可以递归地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)
        })
    }
}

推荐阅读