Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带持续时间的Swift-CoreAnimation_Ios_Swift_Core Animation - Fatal编程技术网

Ios 带持续时间的Swift-CoreAnimation

Ios 带持续时间的Swift-CoreAnimation,ios,swift,core-animation,Ios,Swift,Core Animation,试图理解CoreAnimation和完成处理程序。 为什么这段代码一次发生,而不是超过5.0秒? 我试图让它运行6次,每次持续5秒,总共30秒,并且每5秒打印一次“完成” func animateBox() { UIView.animate(withDuration: 5.0, delay: 0.0, options: [], animations: { self.myBox.layer.borderColor = self.getRandom(

试图理解CoreAnimation和完成处理程序。
为什么这段代码一次发生,而不是超过5.0秒?
我试图让它运行6次,每次持续5秒,总共30秒,并且每5秒打印一次“完成”

    func animateBox() {

        UIView.animate(withDuration: 5.0, delay: 0.0, options: [], animations: {
            self.myBox.layer.borderColor = self.getRandom()
        }, completion: {finished in
            print("complete")
        })
    
    }

    @objc func buttonTapped() {
        
        for _ in 1...6 {
            animateBox()
            print("animate")
        }
    }

for uu in 1…6{
立即执行,因此
animateBox
在一瞬间被调用6次,一次又一次

您要做的是调用前一个动画块的完成处理程序(动画完成时调用)中的每个动画块。类似于以下内容:

UIView.animate(withDuration: 5.0, delay: 0.0, options: [], animations: {
    self.myBox.layer.borderColor = self.getRandom()
}, completion: { finished in
    print("complete")

    UIView.animate(withDuration: 5.0, delay: 0.0, options: [], animations: {
        self.myBox.layer.borderColor = self.getRandom()
    }, completion: { finished in
        print("complete")

        UIView.animate(withDuration: 5.0, delay: 0.0, options: [], animations: {
            self.myBox.layer.borderColor = self.getRandom()
        }, completion: { finished in
            print("complete")

            ...
        })
    })
})
但这将导致一个巨大的完成金字塔…相反,请尝试使用
animateKeyframes

let totalDuration = CGFloat(30)
let relativeIndividualDuration = CGFloat(1) / CGFloat(6)

UIView.animateKeyframes(withDuration: totalDuration, delay: 0, options: .calculationModeCubic, animations: {
    UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: relativeIndividualDuration) {
        self.myBox.layer.borderColor = self.getRandom()
    }

    UIView.addKeyframe(withRelativeStartTime: relativeIndividualDuration, relativeDuration: relativeIndividualDuration) {
        self.myBox.layer.borderColor = self.getRandom()
    }

    UIView.addKeyframe(withRelativeStartTime: relativeIndividualDuration * 2, relativeDuration: relativeIndividualDuration) {
        self.myBox.layer.borderColor = self.getRandom()
    }

    ...
})

对动画进行排序的另一种方法是使用delay参数,并在第一步之后为每个步骤添加延迟:

func animateBox(step: Int) {
    let duration = 5.0
    UIView.animate(withDuration: duration, 
      delay: duration * Double(step), 
      options: [], 
      animations: {
        self.myBox.layer.borderColor = self.getRandom()
    }, completion: {finished in
        print("complete")
    })

}

@objc func buttonTapped() {
    
    for index in 0...5 {
        animateBox(step: index)
        print("animating step \(index)")
    }
}
(注意,我将您的循环更改为从0…5开始运行,因此
delay=step*duration
将从0开始。)