快速背景色动画循环

问题描述 投票:0回答:5

我希望我的 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)

    })
ios swift loops animation colors
5个回答
19
投票

试试这个:

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.animate(withDuration: 2, delay: 0.0, options:[UIView.AnimationOptions.repeat, UIView.AnimationOptions.autoreverse], animations: {
     self.view.backgroundColor = UIColor.black
     self.view.backgroundColor = UIColor.green
     self.view.backgroundColor = UIColor.darkGray
     self.view.backgroundColor = UIColor.red
}, completion: nil)

3
投票

下面的代码有助于实现用户与动画视图的交互。随机颜色生成(在需要时使用)。

UIView.animateWithDuration(7, delay: 1, options: [UIViewAnimationOptions.AllowUserInteraction,
UIViewAnimationOptions.Repeat,
UIViewAnimationOptions.Autoreverse],
animations: {
               self.yourView.backgroundColor = self.randomColor()
               self.yourView.backgroundColor = self.randomColor()
            }, completion:nil)

func randomColor() -> UIColor {
   let randomRed:CGFloat = CGFloat(drand48())
   let randomGreen:CGFloat = CGFloat(drand48())
   let randomBlue:CGFloat = CGFloat(drand48())
   return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}

2
投票

你必须使用

NSTimer

let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: true)

func update() {
    let nextCollor = getNextColor()
    UIView.animateWithDuration(X.0, animations: {
        self.view.backgroundColor = nextCollor
    })
}

func getNextColor() -> UIColor {
    let currentColor = self.view.backgroundColor

    if currentColor == smaple1 {
        return UIColor.redColor()
    } else if currentColor == smaple2 {
        return UIColor.grayColor()
    } else {
        return UIColor.whiteColor()
    }
}

NSTimer.scheduledTimerWithTimeInterval
每 5 秒运行一次代码

PS:完成后不要忘记使计时器无效。只需拨打

timer.invalidate()
即可。否则你会崩溃。


1
投票

斯威夫特5

override func viewWillAppear(_ animated: Bool) {
    self.customButton.backgroundColor = .red
    UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse], animations: {
        self.customButton.backgroundColor = .none
    }, completion: nil)

}

0
投票

我有动态/未知数量的颜色可以循环,所以我稍微重构了 Swift 5 答案:

斯威夫特5

            UIView.animate(withDuration: 1.0, delay: 0.0, options: [.repeat, .autoreverse]) {
                // colors is the dynamic [UIColor] array previously defined
                for color in colors {
                    self.colorView.backgroundColor = color
                }
            } completion: { (finished) in
                
            }

这将通过淡入淡出式动画很好地循环颜色 - 并且使用 .autoreverse 选项,它不会“捕捉”到起始颜色。

© www.soinside.com 2019 - 2024. All rights reserved.