[Swift通天遁地]八、媒体与动画-(10)在项目中播放GIF动画

本文将演示使用第三方类库播放GIF动画。

首先确保已经安装了所需的第三方类库。双击查看安装配置文件【Podfile】

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'SwiftGifOrigin', '~> 1.6.1'
7 end

根据配置文件中的相关设置,安装第三方类库。

安装完成之后,双击打开项目文件【DemoApp.xcodeproj】

往项目中导入GIF文件。

在左侧的项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 //引入已经安装的第三方类库
 3 import SwiftGifOrigin
 4 
 5 class ViewController: UIViewController {
 6 
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view, typically from a nib.
10         
11         //初始化一个图像视图作为动画的载体。
12         let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 280, height: 170))
13         //调用图像视图对象的扩展方法,读取项目中的动画文件。
14         imageView.loadGif(name: "fire")
15         //将图像视图对象移动到根视图的中心位置
16         imageView.center = self.view.center
17         
18         //将图像视图添加到根视图
19         self.view.addSubview(imageView)
20         //设置根视图的背景颜色为黑色
21         self.view.backgroundColor = UIColor.black
22     }
23 
24     override func didReceiveMemoryWarning() {
25         super.didReceiveMemoryWarning()
26         // Dispose of any resources that can be recreated.
27     }
28 }

猜你喜欢

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