[Xcode 实际操作]六、媒体与动画-(11)UIView视图卷曲动画的制作

目录:[Swift]Xcode实际操作

本文将演示UIView视图卷曲动画的制作。

在项目导航区,打开视图控制器的代码文件【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         //创建一个位置在(50,50),尺寸为(220,320)的显示区域
10         let rect = CGRect(x: 50, y: 50,  220, height: 320)
11         //初始化一个图像视图,并设置其位置和尺寸信息
12         let imageView = UIImageView(frame: rect)
13 
14          //从项目资源文件中加载一张图片
15         let image = UIImage(named: "Picture")
16         //给图像视图指定需要显示的图片
17         imageView.image = image
18         //设置图像视图的标识值,以方便后期对图像视图的调用
19         imageView.tag = 1
20         
21         //将图像视图,添加到当时视图控制器的根视图
22         self.view.addSubview(imageView)
23         
24         //初始化一个按钮对象,当点击按钮时,开始播放动画
25         let button = UIButton(type: UIButton.ButtonType.system)
26         //设置按钮对象的位置为(50,400),尺寸为(220,44)
27         button.frame = CGRect(x: 50, y: 400,  220, height: 44)
28         //设置按钮对象的背景颜色为橙色
29         button.backgroundColor = UIColor.orange
30         //设置按钮对象的标题文字
31         button.setTitle("Tap it.", for: .normal)
32         //给按钮对象,绑定点击事件
33         button.addTarget(self, action: #selector(ViewController.playAnimation), for: UIControl.Event.touchUpInside)
34         
35         //将按钮对象,添加到当前视图控制器的根视图
36         self.view.addSubview(button)
37     }
38     
39     //创建一个方法,用来响应按钮的点击事件
40     @objc func playAnimation()
41     {
42         //发出开始动画的请求,
43         //标志着视图动画块的开始。
44         //在它和提交动画请求之间,可以定义动画的各种展现方式
45         UIView.beginAnimations(nil, context: nil)
46         //设置动画的播放速度为淡入淡出
47         UIView.setAnimationCurve(.easeInOut)
48         //设置动画的时长为5秒
49         UIView.setAnimationDuration(5)
50         //设置动画从视图当前状态开始播放
51         UIView.setAnimationBeginsFromCurrentState(true)
52         //通过标识值,找到上方代码种创建的图像视图作为动画的载体
53         let view = self.view.viewWithTag(1)
54          //设置动画类型为卷曲动画
55         UIView.setAnimationTransition(.curlUp, for: view!, cache: true)
56         //调用视图的提交动画方法,标志着动画块的结束
57         UIView.commitAnimations()
58     }
59 
60     override func didReceiveMemoryWarning() {
61         super.didReceiveMemoryWarning()
62         // Dispose of any resources that can be recreated.
63     }
64 }
原文地址:https://www.cnblogs.com/strengthen/p/10034777.html