iOS 中的 UIView 动画编程其实还是很简单的,像 CSS3 一样,在给定的时间内完成状态连续性的变化呈现。比如背景色,Frame 大小,位移、翻转,特明度等。
以前我使用的编程方式都是用下面那样的三段式处理:
1
2
3
4
5
6
7
8
|
[ 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 代码的同志会比较适应,还是来个简单的代码片段:
1
2
3
4
5
|
[ UIView animateWithDuration:1.0
animations:^{
self .view.alpha = 0.0;
self .view.frame = CGRectMake(10, 10, 50, 50);
}];
|
对于不太复杂的动画,上面的写法很精练,因为只有一条语句,如果动画太过复杂了,写在这样一条语句中就会显得冗长了,对于代码调试没那么方便。
animateWithDuration 有三个重载方法:
比如我们用最后面那个重载方法,可以比 beginAnimations...commitAnimations 更轻松的实现动画完后执行的动作,如:
01
02
03
04
05
06
07
08
09
10
|
[ 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 该如何实现上面同样的行为:
01
02
03
04
05
06
07
08
09
10
11
12
13
|
[ 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:
1
2
3
|
-( void ) animationStopped {
NSLog (@ "Do something after the animation." );
}
|
代码是多些,但是 beginAnimations...commitAnimations 编程方式是全能的,而 animateWithDuration 是有局限性的,因为它的目的就是让编写代码更简洁。在 animateWithDuration 中只提供了 completion 代码块,意即在动画完成后执行的动作,而要达成
[UIView setAnimationWillStartSelector:@selector(animationWillStart)]
这样的在动画即将启动之前的动作却是无能为力,还有些动画设置也是 animateWithDuration 做不到。所以一旦你先前用 animateWithDuration 实现的动画方式要增加稍复杂的功能而不得不用 beginAnimations...commitAnimations 来改写时,付出就大了。
该用哪种方式,只能自己斟酌吧。
分享到:
相关推荐
UIView 动画 UIView动画
自定义UIView动画 http://blog.csdn.net/brycegao321/article/details/53817222
uiView 动画翻转的效果,显示前后两个视图替换显示
UIView 震动动画效果 AFViewShaker ,AFViewShaker 是一个简单的工具用来执行 。 使用方法: AFViewShaker *...
-uiview动画 -CoreAnimation基本动画 -关键帧动画 -TransferAnimation 文章对应的开发说明 http://liuyanwei.jumppo.com/2015/10/30/iOS-Animation-UIViewAndCoreAnimation.html 如果大家支持,请在github...
主要介绍了iOS开发中的转场动画和组动画以及UIView封装动画,主要用到了CAAnimation类和UIView类,需要的朋友可以参考下
xcode6,simulator-iPhone 6
主要介绍了IOS 开发之UIView动画的实例详解的相关资料,需要的朋友可以参考下
UIView的动画
iOS中UIView的翻页动画demo 有几个是私有的api,小心使用。
DaisyChain - 一个微框架让UIView动画链接?变得超级简单
KTOneFingerRotationGestureRecognizer 实现了自定义的 UIGestureRecognizer 用于实现单手指旋转操作,同时会跟踪手指的移动。
简单的转场动画,包括高仿QQ电话路径动画 CABasicAnimation动画等动画组合(想了解动画的童鞋可以研究研究)、常见的气泡转场动画(UIView动画,好多项目都在用哦)、以及翻页转场、模拟系统的导航、模态动画(对想...
UIViewRecorder-ObjC 用于将 UIView 动画帧保存为 PNG 或 JPEG 的Objective C 端口
自己写了一个Dome 实现了UIView或者时UIViewController 缩放动画效果
iOS 中对UIView简单的封装,换个说法:运用帧动画封装了一个HUD,使用起来简单方便,只需要调用 [RKJAnimatingHUD RKJStarAnimatingForView:self];或者 [RKJAnimatingHUD RKJStopAnimatingForView:self];一个类方法...
demo中是创建动画常用的两类方法,一种是基于CAAnimation,一种是基于UIView.
ios中关于uiview