UIView动画(过渡效果)的学习笔记

来源:互联网 发布:日立电梯调试软件 编辑:程序博客网 时间:2024/06/05 00:30

UIView视图的动画功能,可以使在更新或切换视图时有放缓节奏、产生流畅的动画效果,进而改善用户体验。UIView可以产生动画效果的变化包括:

位置变化:在屏幕上移动视图。

  • 大小变化:改变视图框架(frame)和边界。
  • 拉伸变化:改变视图内容的延展区域。
  • 改变透明度:改变视图的alpha值。
  • 改变状态:隐藏或显示状态。
  • 改变视图层次顺序:视图哪个前哪个后。
  • 旋转:即任何应用到视图上的仿射变换(transform)。
动画种类很多,如下

一.基本方式:使用UIView类的UIViewAnimation扩展
 UIView动画是成块运行的。发出beginAnimations:context:请求标志着动画块的开始;commitAnimations标志着动画块的结束。把这两个类方法发送给UIView而不是发送给单独的视图。在这两个调用之间的可定义动画的展现方式并更新视图。函数说明:
//开始准备动画+ (void)beginAnimations:(NSString *)animationID context:(void *)context;//运行动画+ (void)commitAnimations;
具体实现
[UIView beginAnimations:nil context:nil];
//setAnimationCurve来定义动画加速或减速方式
[UIView setAnimaitonCurve:UIViewAnimationCurveLinear]; 
[UIView setAnimationDuration:2.7]; //动画时长
[UIView setAnimationTransition:transition forView:self.view cache:YES];
// operation>>>
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// end<<<<<
[UIView commitAnimations];

or


CGContextRef context = UIGraphicsGetCurrentContext(); //返回当前视图堆栈顶部的图形上下文
[UIView beginAnimations:nil context:context];
[UIView setAnimaitonCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
// View changes go here
[contextView setAlpha:0.0f];
[UIView commitAnimations];

二.block方式:使用UIView类的UIViewAnimationWithBlocks扩展
要用到的函数有:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);//间隔,延迟,动画参数(好像没用?),界面更改块,结束块+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL
复制代码
复制代码
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // toView added to fromView.superview, fromView removed from its superview界面替换,这里的options参数有效

三.core方式:使用CATransition类
iPhone还支持Core Animation作为其QuartzCore架构的一部分,CA API为iPhone应用程序提供了高度灵活的动画解决方案。但是须知:CATransition只针对图层,不针对视图。图层是Core Animation与每个UIView产生联系的工作层面。使用Core Animation时,应该将CATransition应用到视图的默认图层([myView layer]而不是视图本身。

使用CATransition类实现动画,只需要建立一个Core Animation对象,设置它的参数,然后把这个带参数的过渡添加到图层即可。
使用要引入QuartzCore.framework 代码#import <QuartzCore/QuartzCore.h>

 1 CATransition *transition = [CATransition animation]; 2 transition.duration = 0.7; 3 transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 4 transition.type = kCATransitionMoveIn;//{kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade}; 5  6 //更多私有{@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"}; 7 transition.subtype = kCATransitionFromLeft;//{kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom}; 8  9 transition.delegate = self;10 [self.view.layer addAnimation:transition forKey:nil];11 12 // 要做的13 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
CATransition动画使用了类型type和子类型subtype两个概念。type属性指定了过渡的种类(淡化、推挤、揭开、覆盖)。subtype设置了过渡的方向(从上、下、左、右)。另外,CATransition私有的动画类型有(立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关)。


1 0
原创粉丝点击