ios – 快速背景颜色动画循​​环

前端之家收集整理的这篇文章主要介绍了ios – 快速背景颜色动画循​​环前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望我的iOS应用程序的背景颜色在x秒内在四种颜色之间切换

这就是我到目前为止(当我只指定2种颜色时,它正是我想要的)

我还需要动画无限循环运行.

ViewController.swift

UIView.animateWithDuration(X.0,animations: {
        // Color 1
        self.view.backgroundColor = UIColor(rgba)
        // Color 2
        self.view.backgroundColor = UIColor(rgba)
        // Color 3
        self.view.backgroundColor = UIColor(rgba)
        // Color 4
        self.view.backgroundColor = UIColor(rgba)

    })

解决方法

试试这个:
UIView.animateWithDuration(1.0,animations: { () -> Void in
    self.view.backgroundColor = UIColor.blackColor()
    }) { (Bool) -> Void in
        UIView.animateWithDuration(1.0,animations: { () -> Void in
            self.view.backgroundColor = UIColor.greenColor()
            },completion: { (Bool) -> Void in
                UIView.animateWithDuration(1.0,animations: { () -> Void in
                    self.view.backgroundColor = UIColor.grayColor()
                    },completion: { (Bool) -> Void in
                        UIView.animateWithDuration(1.0,animations: { () -> Void in
                            self.view.backgroundColor = UIColor.redColor()
                            },completion:nil)
                })
        })
}

如果你想要一个连续的重复动画,试试这个:

UIView.animateWithDuration(2,delay: 0.0,options:[UIViewAnimationOptions.Repeat,UIViewAnimationOptions.Autoreverse],animations: {
    self.view.backgroundColor = UIColor.blackColor()
    self.view.backgroundColor = UIColor.greenColor()
    self.view.backgroundColor = UIColor.grayColor()
    self.view.backgroundColor = UIColor.redColor()
},completion: nil)
原文链接:https://www.f2er.com/iOS/332504.html

猜你在找的iOS相关文章