[Xcode 實際操做]6、媒體與動畫-(9)使用CATransaction Push製做入場動畫

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

本文將演示如何製做入場動畫。swift

在項目導航區,打開視圖控制器的代碼文件【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          //建立一個位置在(0,100),尺寸爲(320,211)的顯示區域
10         let rect = CGRect(x: 0, y: 100, width: 320, height: 211)
11         //初始化一個圖像視圖,並設置其位置和尺寸信息
12         let imageView = UIImageView(frame: rect)
13         
14         //從項目資源文件中加載一張圖片
15         let image = UIImage(named: "Picture")
16         //給圖像視圖指定須要顯示的圖片
17         imageView.image = image
18         
19         //將圖像視圖,添加到當時視圖控制器的根視圖
20         self.view.addSubview(imageView)
21         
22         //可使用兩種方法來實現動畫效果
23         //方法一:視圖層面的
24         //方法二:使用過渡動畫
25         //它實現了層的過渡動畫,所以能夠進行更低層次的控制
26         //初始化一個過渡動畫實例
27         let animation = CATransition()
28         //設置動畫的時長爲2秒
29         animation.duration = 2
30         //設置動畫的播放速度爲由慢至快
31         animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
32          //設置動畫的類型爲入場動畫
33         animation.type = CATransitionType.push
34         
35         //將動畫指定給圖像視圖的層
36         imageView.layer.add(animation, forKey: "Push")
37     }
38 
39     override func didReceiveMemoryWarning() {
40         super.didReceiveMemoryWarning()
41         // Dispose of any resources that can be recreated.
42     }
43 }
相關文章
相關標籤/搜索