Swift - 动画效果的实现方法总结(附样例)

Swift - 动画效果的实现方法总结(附样例),第1张

概述在iOS中,实现动画有两种方法。一个是统一的animateWithDuration,另一个是组合出现的beginAnimations和commitAnimations。这三个方法都是类方法。 一,使用animateWithDuration来实现动画 (1)此方法共有5个参数: duration:动画从开始到结束的持续时间,单位是秒 delay:动画开始前等待的时间 options:动画执行的选项。 在iOS中,实现动画有两种方法。一个是统一的animateWithDuration,另一个是组合出现的beginAnimations和commitAnimations。这三个方法都是类方法。
一,使用animateWithDuration来实现动画@H_403_8@ (1)此方法共有5个参数: 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 @H_502_137@ 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 import UIKit class VIEwController : UIVIEwController { //游戏方格维度 var dimension: Int = 4 //数字格子的宽度 @H_419_274@wIDth: CGfloat = 50 //格子与格子的间距 padding: = 6 //保存背景图数据 backgrounds: Array < UIVIEw >! overrIDe func vIEwDIDLoad() { super .vIEwDIDLoad() self .backgrounds = >() setupGameMap() playAnimation() } setupGameMap() { x: = 50 y: = 150 for i in 0..<dimension { print (i) y = 150 _ 0..<dimension { //初始化视图 let background = (frame: CGRectMake (x,y,wIDth,wIDth)) background.backgroundcolor = UIcolor .darkGraycolor() .vIEw.addSubvIEw(background) //将视图保存起来,以备后用 backgrounds.append(background) y += padding + wIDth } x += padding+wIDth } } playAnimation() { tile backgrounds{ //先将数字块大小置为原始尺寸的 1/10 tile.layer.setAffinetransform( CGAffinetransformMakeScale (0.1,0.1)) //设置动画效果,动画时间长度 1 秒。 .animateWithDuration(1,delay:0.01, options: UIVIEwAnimationoptions . TransitionNone ,animations: { ()-> VoID in //在动画中,数字块有一个角度的旋转。 CGAffinetransformMakeRotation (90)) }, completion:{ (finished: Bool ) -> in in //完成动画时,数字块复原 CGAffinetransformIDentity ) }) }) } } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } }

(5)样例2:只有从小变大的效果 22
playAnimation() { backgrounds{ //先将数字块大小置为原始尺寸的 1/10 //设置动画效果,动画时间长度 1 秒。 { in (1,1)) completion:{ in .animateWithDuration(0.08,animations:{ in ) }) }) }

(6)样例3:方块从不透明到透明的效果 20
tile.Alpha = 0; //设置动画效果,动画时间长度 1 秒。 CurveEaseInOut 二,使用beginAnimations和commitAnimations方法来实现动画@H_403_8@ beginAnimations:此方法开始一个动画块,调用commitAnimations结束一个动画块,并且动画块是允许嵌套的。 commitAnimations:此方法用于结束一个动画块,动画是在一个独立的线程中运行的,动画在生效时,所有应用程序不会中断。 在beginAnimations和commitAnimations中间的代码中,我们可以设置各种动画的属性。比如持续时间,使用哪种预置的动画效果等。

(1)淡入,淡出,移动,改变大小动画 24
//淡出动画 .beginAnimations( nil ) .setAnimationDuration(2.0) imageVIEw.Alpha = 0.0 .commitAnimations() //淡入动画 @H_419_274@) .setAnimationDuration(2.0) imageVIEw.Alpha = 1.0 .commitAnimations() //移动动画 ) .setAnimationDuration(2.0) imageVIEw.center = CGPointMake (250,250) .setAnimationCurve( UIVIEwAnimationCurve EaSEOut ) //设置动画相对速度 .commitAnimations() //大小调整动画 ) .setAnimationDuration(2.0) imageVIEw.frame = (100,180,50,50) .commitAnimations()

(2)两个视图切换的过渡动画
uiviewanimationtransition定义了5种过渡动画类型: None:无过渡动画效果 FlipFromleft:从左侧向右侧翻转 FlipFromright:从右侧向左侧翻转 CurlUp:向上卷数翻页 CurlDown:向下翻页
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 import UIKit class VIEwController : UIVIEwController { overrIDe func vIEwDIDLoad() { super .vIEwDIDLoad() @H_419_274@ //创建一个按钮,用来点击播放动画 let button: UIbutton = (type:. System ) button.frame= CGRectMake (10,100,30) button.setTitle( "播放动画" UIControlState . normal ) button.addTarget( self Selector ( "play" ),forControlEvents:. touchUpInsIDe ) .vIEw.addSubvIEw(button); //添加两个红蓝视图 redVIEw: UIVIEw (frame: (50,150,400)) redVIEw.backgroundcolor = UIcolor .redcolor() .vIEw.insertSubvIEw(redVIEw,atIndex: 0) blueVIEw: .bluecolor() .vIEw.insertSubvIEw(blueVIEw,atIndex: 1) } //切换视图并播放动画 play(){ .beginAnimations( nil ) @H_404_1109@.setAnimationDuration(3.0) .setAnimationTransition(. CurlUp .vIEw,cache: true ) .vIEw.exchangeSubvIEwAtIndex(1,withSubvIEwAtIndex: 0) .commitAnimations() } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } }

(3)页面或元件翻转效果 18 19 20 21 22 23 24 30
vIEwDIDLoad() { .vIEwDIDLoad() @H_419_274@ //创建一个按钮,用来点击播放动画 button: UIbutton = (type:. System ) button.frame= "播放动画" UIControlState normal ) button.addTarget( Selector ( "play" touchUpInsIDe ) .vIEw.addSubvIEw(button); } //切换视图并播放动画 play(){ //将整个主视图面板实现一个翻转效果 .beginAnimations( "animation" ) .setAnimationDuration(2) EaseInOut ) .setAnimationTransition(. FlipFromleft false ) .commitAnimations() } dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() } }
总结

以上是内存溢出为你收集整理的Swift - 动画效果的实现方法总结(附样例)全部内容,希望文章能够帮你解决Swift - 动画效果的实现方法总结(附样例)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1079621.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)