[Xcode10 實際操作]六、媒體與動畫-(13)使用UIImageView製作幀動畫
阿新 • • 發佈:2018-11-29
本文將演示如何將匯入的序列圖片,轉換為幀動畫。
在專案導航區開啟資原始檔夾【Assets.xcassets】
【+】->【Import】->選擇圖片->【Open】
在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】
1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib.8 9 //初始化一個數組,用來存放圖片素材 10 var images = [UIImage]() 11 //建立一個迴圈, 12 for i in 1 ... 19 13 { 14 //將匯入的圖片,依次存入陣列中 15 images.append(UIImage(named: "animation\(i)")!) 16 } 17 18 //初始化一個位置為(0,60),尺寸為(335,253)的影象檢視19 let imageView = UIImageView(frame: CGRect(x: 0, y: 60, width: 335, height: 253)) 20 //設定影象檢視的動畫影象屬性 21 imageView.animationImages = images 22 //設定幀動畫的時長為5秒 23 imageView.animationDuration = 5 24 //設定動畫迴圈次數,0為無限迴圈播放 25 imageView.animationRepeatCount = 026 //開始幀動畫的播放 27 imageView.startAnimating() 28 29 //將影象檢視,新增到當前檢視控制器的根檢視 30 self.view.addSubview(imageView) 31 } 32 33 override func didReceiveMemoryWarning() { 34 super.didReceiveMemoryWarning() 35 // Dispose of any resources that can be recreated. 36 } 37 }