import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let view1=UIView(frame: CGRect(x:10, y:50, width:200, height:200))
let view2=UIView(frame: CGRect(x:60, y:100, width:200, height:200))
let view3=UIView(frame: CGRect(x:90, y:150, width:200, height:200))
view3.layer.cornerRadius=20
view3.layer.borderWidth=2
view3.layer.masksToBounds=true
view1.backgroundColor=UIColor.red
view2.backgroundColor=UIColor.green
view3.backgroundColor=UIColor.orange
self.view.addSubview(view1)
self.view.addSubview(view2)
self.view.addSubview(view3)
UIView.animate(withDuration: 2,
delay: 1, options: .curveEaseInOut, animations: {() -> Void in
view1.frame=CGRect(x:10, y:50, width:300, height:300)
view1.backgroundColor=UIColor.blue
}){(finished) -> Void in
UIView.animate(withDuration: 2, animations: {
view1.frame = CGRect(x:10, y:50, width:200, height:200)
})
UIView.animate(withDuration: 2, delay: 2, options: .curveEaseInOut, animations: {
view1.frame = CGRect(x:10, y:250, width:200, height:200)
}, completion: { (true) in
})
}
UIView.animate(withDuration: 3) {
view2.backgroundColor = UIColor.red
// 如果我们想让一个属性在 animations 闭包中执行,但是又不要执行动画,可以这样
// 放在 performWithoutAnimation 闭包中就会不执行动画了, 这个在有时候做项目的时候某个功能总是会莫名其妙的调一下或者执行一个很奇怪的动画,这时候可以把那段 code 放在这个闭包中,就不会有动画了
UIView.performWithoutAnimation {
view3.alpha = 0.7
}
}
// view的 transform 非常简单,也比较常用,就旋转平移缩放,可以叠加在一起使用
UIView.animate(withDuration: 3, animations: {
view3.transform = CGAffineTransform.identity
.translatedBy(x: -100, y: 0)
.rotated(by:CGFloat(Double.pi/4))
.scaledBy(x: 0.5, y: 0.5)
}) { (true) in
UIView.animate(withDuration: 3) {
// 转回去
view3.transform = CGAffineTransform.identity
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}