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


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

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

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

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

    一.基本方式:使用UIView类的UIViewAnimation扩展
     UIView动画是成块运行的。发出beginAnimations:context:请求标志着动画块的开始;commitAnimations标志着动画块的结束。把这两个类方法发送给UIView而不是发送给单独的视图。在这两个调用之间的可定义动画的展现方式并更新视图。函数说明:

    //开始准备动画
    + (void)beginAnimations:(NSString *)animationID context:(void *)context;

    //运行动画
    + (void)commitAnimations;

    具体二段动画代码:

     1 [UIView beginAnimations:nil context:nil];
    2 //setAnimationCurve来定义动画加速或减速方式
    3 [UIView setAnimaitonCurve:UIViewAnimationCurveLinear];
    4 [UIView setAnimationDuration:2.7]; //动画时长
    5 [UIView setAnimationTransition:transition forView:self.view cache:YES];
    6 // operation>>>
    7 [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    8 // end<<<<<
    9 [UIView commitAnimations];
    10
    11 CGContextRef context = UIGraphicsGetCurrentContext(); //返回当前视图堆栈顶部的图形上下文
    12 [UIView beginAnimations:nil context:context];
    13 [UIView setAnimaitonCurve:UIViewAnimationCurveEaseInOut];
    14 [UIView setAnimationDuration:1.0];
    15 // View changes go here
    16 [contextView setAlpha:0.0f];
    17 [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参数有效

    三.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私有的动画类型有(立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关)。
     

  • 相关阅读:
    安装xshell6
    eclipse的安装和汉化
    collectd+infludb+grafana实现tomcat JVM监控
    百度网站统计和CNZZ网站统计对比
    shell,计算指定行的和,计算指定列的和
    我为什么要写博客
    kafka监控之topic的lag情况监控
    用rundeck启动tomcat报错
    xwiki升级8.8.4
    矩阵掩膜操作
  • 原文地址:https://www.cnblogs.com/lovecode/p/2237077.html
Copyright © 2020-2023  润新知