sau*_*tam 11
对于iOS
UIViewAnimationOptions集提供了不同的方便选项,以实现美观和复杂动画的组合.对于您的特定情况,您将需要两个选项.
UIViewAnimationOptions.Repeat
UIViewAnimationOptions.AutoReverse
Run Code Online (Sandbox Code Playgroud)
请查看以下代码以了解实施情况.
代码:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = UIColor.blueColor()
self.view.addSubview(view)
UIView.animateWithDuration(1,
delay: 0,
options: [UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.Repeat],
animations: {
view.backgroundColor = UIColor.clearColor()
},
completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
说明: 我创建了一个具有特定框架的视图用于演示目的.
您感兴趣的部分是UIView.animateWithDuration方法.请注意,我[UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat]在options参数中提供了一个数组.
这两个选项足以实现如下所示的平滑且永久循环的动画. https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif
如果您不想反转动画,只需UIViewAnimationOptions.AutoReverse从options参数中的数组中删除即可.你会得到这样的动画.
https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif
对于iOS
让viewSquare你成为问题中蓝色方块的名称.
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat,.autoreverse], animations: {
viewSquare.alpha = 0.0
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
我假设你正在为 iOS 编程。
尝试duration看看什么最适合您:
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
let duration = 0.5
override func viewDidLoad() {
super.viewDidLoad()
self.fadeOut(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func fadeIn(finished: Bool) {
UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 1 } , completion: self.fadeOut)
}
func fadeOut(finished: Bool) {
UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 0 } , completion: self.fadeIn)
}
}
Run Code Online (Sandbox Code Playgroud)