iOS-渐变之间的过渡?

时间:2019-06-24 17:58:34

标签: ios swift animation cagradientlayer

我正在尝试在两个不同的渐变之间无休止地旋转背景色,但是实际过渡的持续时间没有预期的长(持续时间不到一秒钟,每次过渡之间没有延迟)。

var backgroundColours = [CAGradientLayer()]
var backgroundLoop = 0

override func viewDidLoad() {
    super.viewDidLoad()
    backgroundColours = [Colors.init().one, Colors.init().two] // These are two CAGradientLayers but I haven't included their code
    backgroundLoop = 0
    self.animateBackgroundColour()
}

func animateBackgroundColour () {

    backgroundLoop = (backgroundLoop + 1) % (backgroundColours.count)

    UIView.animate(withDuration: 2.0, delay: 2.0, options: .curveLinear, animations: {
        self.view.backgroundColor = UIColor.clear
        let backgroundLayer = self.backgroundColours[self.backgroundLoop]
        backgroundLayer.frame = self.view.frame
        self.view.layer.insertSublayer(backgroundLayer, at: 0)
    }, completion: { (Bool) -> Void in
        self.animateBackgroundColour()
    })

这一切都在ViewController

1 个答案:

答案 0 :(得分:0)

UIView.animate仅对五个专门的 view 属性设置动画。您没有为任何这些属性设置动画。您想要的是 layer 动画(例如CABasicAnimation)。