Swift - 动画效果的实现方法总结(附样例)
- duration:动画从开始到结束的持续时间,单位是秒
- delay:动画开始前等待的时间
- options:动画执行的选项。里面可以设置动画的效果。可以使用UIViewAnimationOptions类提供的各种预置效果
- anmations:动画效果的代码块
- completion:动画执行完毕后执行的代码块
(2)UIView支持动画效果的属性
- frame:此属性包含一个矩形,即边框矩形,此值确定了当前视图在其父视图坐标系中的位置与尺寸
- bounds:也是矩形,边界矩形,它指的是视图在其自己的坐标系中的位置和尺寸,左上角坐标永远是(0,0)
- center:确定视图的中心点在其父视图坐标系中的位置坐标。即定义当前视图在父视图中的位置
- alpha:视图的透明度。(但视图完全透明时,不能响应触摸消息)
- backgroundColor:背景色
- transform:这是一种3×3的变化矩阵。通过这个矩阵我们可以对一个坐标系统进行缩放、平移、旋转以及这两者的任意组操作。
(3)Transform(变化矩阵)的四个常用的变换方法
- CGAffineTransformMake():返回变换矩阵
- CGAffineTransformMakeTranslation():返回平移变换矩阵
- CGAffineTransformMakeScale():返回缩放变换矩阵
- CGAffineTransformMakeRotation():返回旋转变换矩阵
(4)样例1:方块初始缩小为原始尺寸1/10。在1秒的动画中复原到完整大小,同时还伴随旋转效果。



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import UIKit class ViewController : UIViewController { //游戏方格维度 var dimension: Int = 4 //数字格子的宽度 var width: CGFloat = 50 //格子与格子的间距 var padding: CGFloat = 6 //保存背景图数据 var backgrounds: Array < UIView >! override func viewDidLoad() { super .viewDidLoad() self .backgrounds = Array < UIView >() setupGameMap() playAnimation() } func setupGameMap() { var x: CGFloat = 50 var y: CGFloat = 150 for i in 0..<dimension { println (i) y = 150 for j in 0..<dimension { //初始化视图 var background = UIView (frame: CGRectMake (x, y, width, width)) background.backgroundColor = UIColor .darkGrayColor() self .view.addSubview(background) //将视图保存起来,以备后用 backgrounds.append(background) y += padding + width } x += padding+width } } func playAnimation() { for tile in backgrounds{ //先将数字块大小置为原始尺寸的 1/10 tile.layer.setAffineTransform( CGAffineTransformMakeScale (0.1,0.1)) //设置动画效果,动画时间长度 1 秒。 UIView .animateWithDuration(1, delay:0.01, options: UIViewAnimationOptions . TransitionNone , animations: { ()-> Void in //在动画中,数字块有一个角度的旋转。 tile.layer.setAffineTransform( CGAffineTransformMakeRotation (90)) }, completion:{ (finished: Bool ) -> Void in UIView .animateWithDuration(1, animations:{ ()-> Void in //完成动画时,数字块复原 tile.layer.setAffineTransform( CGAffineTransformIdentity ) }) }) } } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
(5)样例2:只有从小变大的效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
func playAnimation() { for tile in backgrounds{ //先将数字块大小置为原始尺寸的 1/10 tile.layer.setAffineTransform( CGAffineTransformMakeScale (0.1,0.1)) //设置动画效果,动画时间长度 1 秒。 UIView .animateWithDuration(1, delay:0.01, options: UIViewAnimationOptions . TransitionNone , animations: { ()-> Void in tile.layer.setAffineTransform( CGAffineTransformMakeScale (1,1)) }, completion:{ (finished: Bool ) -> Void in UIView .animateWithDuration(0.08, animations:{ ()-> Void in tile.layer.setAffineTransform( CGAffineTransformIdentity ) }) }) } } |
(6)样例3:方块从不透明到透明的效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
func playAnimation() { for tile in backgrounds{ tile.alpha = 0; //设置动画效果,动画时间长度 1 秒。 UIView .animateWithDuration(1, delay:0.01, options: UIViewAnimationOptions . CurveEaseInOut , animations: { ()-> Void in }, completion:{ (finished: Bool ) -> Void in UIView .animateWithDuration(1, animations:{ ()-> Void in tile.alpha = 1 }) }) } } |
二,使用beginAnimations和commitAnimations方法来实现动画
- beginAnimations:此方法开始一个动画块,调用commitAnimations结束一个动画块,并且动画块是允许嵌套的。
- commitAnimations:此方法用于结束一个动画块,动画是在一个独立的线程中运行的,动画在生效时,所有应用程序不会中断。
在beginAnimations和commitAnimations中间的代码中,我们可以设置各种动画的属性。比如持续时间,使用哪种预置的动画效果等。
(1)淡入,淡出,移动,改变大小动画
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//淡出动画 UIView .beginAnimations( nil , context: nil ) UIView .setAnimationDuration(2.0) imageView.alpha = 0.0 UIView .commitAnimations() //淡入动画 UIView .beginAnimations( nil , context: nil ) UIView .setAnimationDuration(2.0) imageView.alpha = 1.0 UIView .commitAnimations() //移动动画 UIView .beginAnimations( nil , context: nil ) UIView .setAnimationDuration(2.0) imageView.center = CGPointMake (250, 250) UIView .setAnimationCurve( UIViewAnimationCurve . EaseOut ) //设置动画相对速度 UIView .commitAnimations() //大小调整动画 UIView .beginAnimations( nil , context: nil ) UIView .setAnimationDuration(2.0) imageView.frame = CGRectMake (100,180,50,50) UIView .commitAnimations() |
(2)两个视图切换的过渡动画
UIViewAnimationTransition定义了5种过渡动画类型:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var redView: UIView = UIView (frame: CGRectMake (50,50,150,400)) redView.backgroundColor = UIColor .redColor() self .view.insertSubview(redView, atIndex: 0) var blueView: UIView = UIView (frame: CGRectMake (50,50,150,400)) blueView.backgroundColor = UIColor .blueColor() self .view.insertSubview(blueView, atIndex: 1) UIView .beginAnimations( nil , context: nil ) UIView .setAnimationDuration(4.0) UIView .setAnimationTransition( UIViewAnimationTransition . CurlUp , forView: self .view, cache: true ) self .view.exchangeSubviewAtIndex(1, withSubviewAtIndex: 0) UIView .commitAnimations() |
1
2
3
4
5
6
|
//将整个主视图面板实现一个翻转效果 UIView .beginAnimations( "animation" , context: nil ) UIView .setAnimationDuration(2) UIView .setAnimationCurve( UIViewAnimationCurve . EaseInOut ) UIView .setAnimationTransition( UIViewAnimationTransition . FlipFromLeft , forView: self .view, cache: false ) UIView .commitAnimations() |
Swift - 动画效果的实现方法总结(附样例)的更多相关文章
- Swift - 数组排序方法(附样例)
下面通过一个样例演示如何对数组元素进行排序.数组内为自定义用户对象,最终要实现按用户名排序,数据如下: 1 2 3 4 var userList = [UserInfo]() userList.app ...
- AS3 - 数组Array的几个常用方法(附样例)
AS3 - 数组Array的几个常用方法(附样例) 2015-03-30 10:39发布:hangge浏览:241 Flex/Flash开发中,经常会使用到数组,下面总结了一些数组的常用方法. 1 ...
- IOS开发-UIView之动画效果的实现方法(合集)
http://www.cnblogs.com/GarveyCalvin/p/4193963.html 前言:在开发APP中,我们会经常使用到动画效果.使用动画可以让我们的APP更酷更炫,最重要的是优化 ...
- CSS3 - 新单位vw、vh、vmin、vmax使用详解(附样例)
像 px.em 这样的长度单位大家肯定都很熟悉,前者为绝对单位,后者为相对单位.CSS3 又引入了新单位:vw.vh.vmin.vmax.下面对它们做个详细介绍. 一.基本说明 1,vw.vh.vmi ...
- jquery 实现动画效果(各种方法)
1.show()和hide()和toggle()(这是show和hide的一个综合,一个按钮就实现显示和隐藏) 效果: 代码: <button type="button" c ...
- Swift - 本地消息的推送通知(附样例)
使用UILocalNotification可以很方便的实现消息的推送功能.我们可以设置这个消息的推送时间,推送内容等. 当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息. 1, ...
- Swift - 使用ALAssetsLibrary获取相簿里所有图片,视频(附样例)
1,ALAssetsLibrary介绍 (1)通过创建ALAssetsLibrary的实例可以访问系统Photos里的图片与视频.这里图片不仅包括相机拍摄的照片,还包括从iTunes导入的和从其他设备 ...
- Jquery属性选择器(同时匹配多个条件,与或非)(附样例)
1. 前言 为了处理除了两项不符合条件外的选择,需要用到jquery选择器的多个条件匹配来处理,然后整理了一下相关的与或非的条件及其组合. 作为笔记记录. 2. 代码 <!DOCTYPE htm ...
- [转]Jquery属性选择器(同时匹配多个条件,与或非)(附样例)
1. 前言 为了处理除了两项不符合条件外的选择,需要用到jquery选择器的多个条件匹配来处理,然后整理了一下相关的与或非的条件及其组合. 作为笔记记录. 2. 代码 1 2 3 4 5 6 7 8 ...
随机推荐
- 批量的单向的ssh 认证
<pre name="code" class="python">if [ ! $# -eq 2 ] ;then echo "请输入用户密码 ...
- [置顶] HMM Tutorial 隐马尔科夫模型
有一个月没有写博客了,这一个月系统的学习了HMM model. 上周周五做了个report 感觉还好. 所以把Slide贴上来.
- sql基础,必须会的————随便整理、杂乱无章
1.sqlserver2008r2的安装 2.数据库与表的建立.增加.删除.修改. 3,索引的概念,包括聚集与非聚集的区别.全文索引的建立与如何使用全文索引. 4,重新生成索引,重新组织索引. 5,建 ...
- ASP.NET给Table动态添加删除行,并且得到控件的值
ASP.NET给Table动态添加控件并且得到控件的值 由于跟老师做一个小的项目,可是我自己又不太懂js,所以一直为动态建立表格并且能动态的取值和赋值感到苦恼.起初在网上找到了一些js资源,解决了动态 ...
- [转载] iOS开发分辨率那点事
1 iOS设备的分辨率 iOS设备,目前最主要的有3种(Apple TV等不在此讨论),按分辨率分为两类 iPhone/iPod Touch 普屏分辨率 320像素 x 480像素 Retina ...
- [转]apache下htaccess不起作用,linux,windows详解
可能出现下面这三种的错误可能性: 第一种:启用 rewrite 和 .htaccess 设置 rewrite设置:找到apache的配置文件httpd.conf文件,找到:#LoadModule re ...
- 自己python程序的并行修改
遇到运算量大的程序,学习了下python并行运算的方法,在自己的程序上进行了修改,看看是否可以增加效率.原始代码是: import gt_apps as my_apps f=file('sample. ...
- ZOJ 3483 简单if-else
提醒:答案要约分,不然会错! #include<iostream> #include<cstdio> #include<cstring> #include<a ...
- Cubieboard4卡片式电脑
Cubieboard4 also named CC-A80, is a open source mini PC or single board computer which has ultra-pow ...
- JAVA序列化在IO中读写对象的使用
序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间.序列化是为了解决在对对象流进行读写操作时所引发的问题. 序列 ...