1. 程式人生 > >[Swift通天遁地]八、媒體與動畫-(6)使用開源類庫快速實現滑入動畫

[Swift通天遁地]八、媒體與動畫-(6)使用開源類庫快速實現滑入動畫

star recreate eat spa 實現 use 安裝 安裝配置 div

本文將演示使用第三方類庫,快速實現一個從上向下滑入的動畫。

首先確保已經安裝了所需的第三方類庫。雙擊查看安裝配置文件【Podfile】

1 platform :ios, ‘12.0
2 use_frameworks!
3 
4 target DemoApp do
5     source https://github.com/CocoaPods/Specs.git
6     pod Spring, :git => https://github.com/MengTo/Spring.git
7 end

根據配置文件中的相關設置,安裝第三方類庫。

安裝完成之後,雙擊打開項目文件【DemoApp.xcodeproj】

往項目中導入用於實現動畫的圖片素材。

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

 1 import UIKit
 2 //引入已經安裝的第三方類庫
 3 import Spring
 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 image = UIImage(named:"star") 13 //初始化一個動畫圖像視圖對象 14 let imageView = SpringImageView(image: image) 15 16 //設置圖像視圖的顯示區域 17 imageView.frame = CGRect(x: 0, y: 0, width: 80, height: 80) 18 //將圖像視圖移動到水平方向上的中心位置,
19 imageView.center = CGPoint(x: 160, y: 160) 20 //接著將圖像視圖添加到根視圖。 21 self.view.addSubview(imageView) 22 23 //設置動畫圖像視圖對象的動畫類型, 24 //第三方類庫共提供了26個不同的動畫。 25 imageView.animation = "squeezeDown" 26 //設置動畫圖像視圖對象,可以進行動畫的自動播放。 27 imageView.autostart = true 28 } 29 30 override func didReceiveMemoryWarning() { 31 super.didReceiveMemoryWarning() 32 // Dispose of any resources that can be recreated. 33 } 34 }

[Swift通天遁地]八、媒體與動畫-(6)使用開源類庫快速實現滑入動畫