IOS多媒體-簡單動畫
阿新 • • 發佈:2018-11-22
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad(); test1() } func test1() { let imageVIew = UIImageView(frame: CGRect(x: 50, y: 50, width: 220, height: 320)) imageVIew.image = UIImage(named: "Pic4") imageVIew.tag = 1 self.view.addSubview(imageVIew) let button = UIButton(type: UIButtonType.system) button.frame = CGRect(x: 50, y: 400, width: 220, height: 44 ) button.backgroundColor = UIColor.orange button.setTitle("Tap it.", for: UIControlState()) button.addTarget(self, action: #selector(ViewController.playAnimation), for: UIControlEvents.touchUpInside) self.view.addSubview(button) } @objc func playAnimation(){ //反轉動畫的製作 //發出開始動畫的請求 提交請求期間可以定義動畫的展開方式 UIView.beginAnimations(nil, context: nil) UIView.setAnimationCurve(.easeOut)//淡入淡出 UIView.setAnimationDuration(5)//時間 UIView.setAnimationBeginsFromCurrentState(true)//動畫從當前狀態開始播放 let view = self.view.viewWithTag(1) //檢視在進行翻轉的同時移動到目標位置並縮小至不可見 view?.frame = CGRect(x: 50, y: 50, width: 0, height: 0 ) //是指動畫的代理起為當前根檢視 UIView.setAnimationDelegate(self) //設定動畫結束執行的方法 UIView.setAnimationDidStop(#selector(ViewController.animationStop)) //設定反轉動畫 UIView.setAnimationTransition(.flipFromLeft, for: view!, cache: true) UIView.commitAnimations() //捲曲動畫 // UIView.beginAnimations(nil, context: nil) // UIView.setAnimationCurve(.easeOut)//淡入淡出 // UIView.setAnimationDuration(5)//時間 // UIView.setAnimationBeginsFromCurrentState(true) // let view = self.view.viewWithTag(1) // UIView.setAnimationTransition(.curlUp, for: view!, cache: true) // UIView.commitAnimations() } @objc func animationStop(){ //動畫結束後的操作 //將檢視從父檢視移除 self.view.viewWithTag(1)?.removeFromSuperview() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }