1. 程式人生 > >[Swift通天遁地]八、媒體與動畫-(7)實現多個動畫的順序播放效果

[Swift通天遁地]八、媒體與動畫-(7)實現多個動畫的順序播放效果

end del ase created lan named 持續時間 cgpoint ecs

本文將演示使用第三方類庫,實現多個動畫的順序播放效果。

首先確保已經安裝了所需的第三方類庫。雙擊查看安裝配置文件【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: 200) 20 //將圖像視圖添加到根視圖。 21 self.view.addSubview(imageView) 22 23 //設置動畫圖像視圖對象的動畫類型 24 imageView.animation = "squeezeDown" 25 //設置動畫圖像視圖對象,可以進行動畫的自動播放。 26 imageView.autostart = true 27 //設置動畫效果的作用強度,默認值為1. 28 imageView.force = 0.5 29 //設置動畫效果的緩沖曲線, 30 //第三方類庫支持五種緩沖曲線 31 imageView.curve = "spring" 32 //設置動畫效果的持續時間為1秒 33 imageView.duration = 1.0 34 35 //添加第二段動畫 36 imageView.animateToNext { 37 //設置動畫效果的作用強度 38 imageView.force = 5.0 39 //設置動畫效果的持續時間為2秒 40 imageView.duration = 2.0 41 //設置動畫在2秒鐘之後開始播放 42 imageView.delay = 2.0 43 //設置動畫的重復的次數為2次 44 imageView.repeatCount = 2 45 //設置動畫的緩存曲線 46 imageView.curve = "easeIn" 47 //設置動畫的類型為搖晃動畫 48 imageView.animation = "shake" 49 imageView.animateTo() 50 } 51 } 52 53 override func didReceiveMemoryWarning() { 54 super.didReceiveMemoryWarning() 55 // Dispose of any resources that can be recreated. 56 } 57 }

[Swift通天遁地]八、媒體與動畫-(7)實現多個動畫的順序播放效果