[Xcode10 实际操作]六、媒体与动画-(13)使用UIImageView制作帧动画

本文将演示如何将导入的序列图片,转换为帧动画。

在项目导航区打开资源文件夹【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 = 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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10040480.html