您的位置:首页 > 产品设计 > UI/UE

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

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

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

创建UIView动画(块)——(指过渡效果的动画)

一.基本方式:使用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<<<<<<br> [UIView commitAnimations];
 
 CGContextRef context = UIGraphicsGetCurrentContext(); //返回当前视图堆栈顶部的图形上下文
 [UIView beginAnimations:nil context:context];
 [UIView setAnimaitonCurve:UIViewAnim
4000
ationCurveEaseInOut];
 [UIView setAnimationDuration:1.0];
 // View changes go here
 [contextView setAlpha:0.0f];
 [UIView commitAnimations];

其中transition取值范围:UIView类本身提供四种过渡效果
UIViewAnimationTransitionNone 正常
UIViewAnimationTransitionFlipFromLeft 从左向右翻
UIViewAnimationTransitionFlipFromRight 从右向左翻
UIViewAnimationTransitionCurlUp 从下向上卷
UIViewAnimationTransitionCurlDown 从上向下卷 

二.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参数有效


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

使用CATransition类实现动画,只需要建立一个Core Animation对象,设置它的参数,然后把这个带参数的过渡添加到图层即可。

使用要引入QuartzCore.framework 代码#import

CATransition *transition = [CATransition animation];

 transition.duration = 0.7;

 transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

 transition.type = kCATransitionMoveIn;//{kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};

 

 //更多私有{@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"};

 transition.subtype = kCATransitionFromLeft;//{kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};

 

 transition.delegate = self;

 [self.view.layer addAnimation:transition forKey:nil];

 

 // 要做的

 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

CATransition动画使用了类型type和子类型subtype两个概念。type属性指定了过渡的种类(淡化、推挤、揭开、覆盖)。subtype设置了过渡的方向(从上、下、左、右)。另外,CATransition私有的动画类型有(立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关)。
http://blog.sina.com.cn/s/blog_86eee82e01019wjt.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: