import UIKit
class ViewController: UIViewController {
var myView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
myView = UIView()
myView?.frame = CGRect(x:100,y:100,width:100,height:100)
myView?.backgroundColor = UIColor.green
self.view.addSubview(myView!)
// Do any additional setup after loading the view, typically from a nib.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 简单的过渡动画:后面跟的事尾随闭包
UIView.animate(withDuration: 1) {
self.myView?.backgroundColor = UIColor.red
}
// 嵌套动画:
UIView.animate(withDuration: 3, animations: {
self.myView?.center = CGPoint(x:200,y:400)
}){ (finish) in
self.myView?.backgroundColor = UIColor.gray
}
// 延迟动画:
UIView.animateKeyframes(withDuration: 2, delay: 3, options: [], animations: {
}, completion: nil)
// 阻尼动画:usingSpringWithDamping弹力系数0-1,0的弹力最大、 initialSpringVelocity初始速度
UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: {
}, completion: nil)
// UIView层的转场动画:
// (1)、重构视图:
UIView.transition(with:myView!, duration: 3, options: .transitionCurlUp, animations: {
self.myView?.backgroundColor = UIColor.yellow
}) { (finish) in
}
// (1)、从一个视图转到另一个视图
let otherView = UIView()
otherView.frame = CGRect(x:200,y:100,width:200,height:300)
otherView.backgroundColor = UIColor.red
UIView.transition(from:myView!, to: otherView, duration: 3, options: .transitionFlipFromTop, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}