首页 技术 正文
技术 2022年11月6日
0 收藏 686 点赞 670 浏览 2454 个字

iOS  中的 UIView 动画编程其实还是很简单的,像 CSS3 一样,在给定的时间内完成状态连续性的变化呈现。比如背景色,Frame 大小,位移、翻转,特明度等。

以前我使用的编程方式都是用下面那样的三段式处理:

12345678     [UIView beginAnimations:nil context: nil];    [UIView setAnimationDuration:1.0];         //要动画改变的属性    self.view.alpha = 0.0;    self.view.frame = CGRectMake(10, 10, 50, 50);         [UIView commitAnimations];

那么被 beginAnimations 和  commitAnimations 框起来的代码就会让你产生动画效果,这种方式像是数据库中的事物编程一样。

还有另一种编程方式,使用代码块,对于常常书写 JS 代码的同志会比较适应,还是来个简单的代码片段:

12345     [UIView animateWithDuration:1.0                     animations:^{                         self.view.alpha = 0.0;                         self.view.frame = CGRectMake(10, 10, 50, 50);                     }];

对于不太复杂的动画,上面的写法很精练,因为只有一条语句,如果动画太过复杂了,写在这样一条语句中就会显得冗长了,对于代码调试没那么方便。

animateWithDuration 有三个重载方法:

比如我们用最后面那个重载方法,可以比 beginAnimations…commitAnimations 更轻松的实现动画完后执行的动作,如:

01020304050607080910     [UIView animateWithDuration:1.0                          delay: 0.0                        options: UIViewAnimationOptionCurveEaseIn                     animations:^{                         self.view.alpha = 0.0;                         self.view.frame = CGRectMake(10, 10, 50, 50);                     }                     completion:^(BOOL finished){                         NSLog(@"Do something after the animation.");                     }];

再回头看看 beginAnimations…commitAnimations 该如何实现上面同样的行为:

01020304050607080910111213     [UIView beginAnimations:nil context: nil];    [UIView setAnimationDuration:1.0];         [UIView setAnimationDelay:0.0];    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];         [UIView setAnimationDelegate:self];    [UIView setAnimationDidStopSelector:@selector(animationStopped)];         self.view.alpha = 0.0;    self.view.frame = CGRectMake(10, 10, 50, 50);         [UIView commitAnimations];

还要给当前类加一个方法 animationStopped:

123 -(void) animationStopped {    NSLog(@"Do something after the animation.");}

代码是多些,但是 beginAnimations…commitAnimations 编程方式是全能的,而 animateWithDuration 是有局限性的,因为它的目的就是让编写代码更简洁。在 animateWithDuration 中只提供了 completion 代码块,意即在动画完成后执行的动作,而要达成

[UIView setAnimationWillStartSelector:@selector(animationWillStart)]

这样的在动画即将启动之前的动作却是无能为力,还有些动画设置也是 animateWithDuration 做不到。所以一旦你先前用 animateWithDuration  实现的动画方式要增加稍复杂的功能而不得不用 beginAnimations…commitAnimations 来改写时,付出就大了。

该用哪种方式,只能自己斟酌吧。

关于 UIView 的动画请参考官方文档:Animations

本文链接 http://unmi.cc/uiview-animation-programming/, 来自 隔叶黄莺 Unmi Blog

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905