快速背景色动画循环

时间:2015-10-17 09:53:00

标签: ios swift loops animation colors

我希望我的iOS应用的背景颜色在x秒内在四种颜色之间切换

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

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

ViewController.swift

vibrator.cancel();

5 个答案:

答案 0 :(得分:15)

试试这个:

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)

答案 1 :(得分:3)

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

before_save :format_time 

def format_time
  self.from_time = sanitize_time( from_time )
  self.to_time   = sanitize_time( to_time )
end

def sanitize_time( time_str )
  begin
    DateTime.strptime(time_str, '%H %M') # will raise ArgumentError if not in '%H %M' format
    time_str
  rescue ArgumentError
    time_str.to_time.strftime("%H %M")
  end
end

答案 2 :(得分:2)

您必须使用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

timer.invalidate()每5秒运行一次代码

PS:完成后不要忘记无效计时器。只需致电{{1}}即可。否则你会崩溃。

答案 3 :(得分: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)

}

答案 4 :(得分:0)

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

Swift 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 选项,它不会“捕捉”到起始颜色。