今天尝试做了一个视图的平移动画,碰到一些问题,现在贴出来和大家分享。通过动画效果,可以使我们的App更加的好看,增加用户体验。具体实现如下:
(1)在界面中拖入一个View控件,设置成正方形,并进行填充颜色。然后绑定到代码中;
(2)在类中重写一个viewDidAppear()方法,当界面出现的时候开始执行动画。
import UIKit
class PositionViewController: UIViewController {
@IBOutlet weak var greenSquare: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(1, animations: {
//这里是一个Closure,也就是一个闭包函数;
//平移到X轴上对称的位置;
self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning(