[Xcode 实际操作]六、媒体与动画-(9)使用CATransaction Push制作入场动画

目录:[Swift]Xcode实际操作

本文将演示如何制作入场动画。

在项目导航区,打开视图控制器的代码文件【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          //创建一个位置在(0,100),尺寸为(320,211)的显示区域
10         let rect = CGRect(x: 0, y: 100,  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 }
原文地址:https://www.cnblogs.com/strengthen/p/10034550.html