[Xcode 實際操做]6、媒體與動畫-(13)使用UIImageView製做幀動畫

目錄:[Swift]Xcode實際操做html

本文將演示如何將導入的序列圖片,轉換爲幀動畫。swift

在項目導航區打開資源文件夾【Assets.xcassets】數組

【+】->【Import】->選擇圖片->【Open】app

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】ide

 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 = 0
26         //開始幀動畫的播放
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 }
相關文章
相關標籤/搜索