時間:2017年05月22日星期一
說明:本文部份內容均來自慕課網。@慕課網:http://www.imooc.com
教學示例源碼:https://github.com/zccodere/s...
我的學習源碼:https://github.com/zccodere/s...ios
學習課程前,請先學習慕課網_《iOS-動畫入門》學習總結。git
UIView動畫的Timinggithub
各屬性的原始狀態 各屬性的結束狀態 執行時長 執行過程 執行完畢的處理
UIView動畫的重複執行:Repeatswift
代碼演示:app
// // RepeatViewController.swift // iOSAnimationDemo // // Created by zc on 2017/5/22. // Copyright © 2017年 com.zccoder. All rights reserved. // import UIKit class RepeatViewController: UIViewController { // 藍色方塊 @IBOutlet weak var blueSquare: UIView! // 紅色方塊 @IBOutlet weak var redSquare: UIView! // 綠色方塊 @IBOutlet weak var greenSquare: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) UIView.animate(withDuration: 1, animations: { // 藍色方塊從左邊移動到右邊 self.blueSquare.center.x = self.view.bounds.width - self.blueSquare.center.x }) UIView.animateKeyframes(withDuration: 1, delay: 0, options: .repeat, animations: { // 紅色方塊重複執行從左邊移動到右邊 self.redSquare.center.x = self.view.bounds.width - self.redSquare.center.x }, completion: nil) UIView.animateKeyframes(withDuration: 1, delay: 0, options: [.repeat, .autoreverse], animations: { // 綠色方塊重複執行從左邊移動到右邊而後從右邊移動到左邊 self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x }, completion: nil) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
效果以下:ide
UIView Easing動畫學習
代碼演示:動畫
// // EasingViewController.swift // iOSAnimationDemo // // Created by zc on 2017/5/22. // Copyright © 2017年 com.zccoder. All rights reserved. // import UIKit class EasingViewController: UIViewController { @IBOutlet weak var blueSquare: UIView! @IBOutlet weak var redSquare: UIView! @IBOutlet weak var greenSquare: UIView! @IBOutlet weak var yellowSquare: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) UIView.animate(withDuration: 1, animations: { // 藍色方塊從左邊移動到右邊-均速移動 self.blueSquare.center.x = self.view.bounds.width - self.blueSquare.center.x }) UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { // 紅色方塊從左邊移動到右邊-先慢後快 self.redSquare.center.x = self.view.bounds.width - self.redSquare.center.x }, completion: nil) UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: { // 綠色方塊從左邊移動到右邊-先快後慢 self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x }, completion: nil) UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: { // 黃色方塊從左邊移動到右邊-兩邊快中間慢 self.yellowSquare.center.x = self.view.bounds.width - self.yellowSquare.center.x }, completion: nil) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
效果以下:spa
UIView Spring動畫:Spring:相似彈簧code
代碼演示:
// // SpringViewController.swift // iOSAnimationDemo // // Created by zc on 2017/5/22. // Copyright © 2017年 com.zccoder. All rights reserved. // import UIKit class SpringViewController: UIViewController { @IBOutlet weak var blueSquare: UIView! @IBOutlet weak var redSquare: UIView! @IBOutlet weak var greenSquare: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // 藍色方塊從左邊移動到右邊-均速移動 UIView.animate(withDuration: 1, animations: { self.blueSquare.center.x = self.view.bounds.width - self.blueSquare.center.x }) // 紅色方塊從左邊移動到右邊-彈簧效果 UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: [], animations: { self.redSquare.center.x = self.view.bounds.width - self.redSquare.center.x }, completion: nil) // 綠色方塊從左邊移動到右邊-先快後慢 UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 1, options: [], animations: { self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x }, completion: nil) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
效果以下: