zoukankan      html  css  js  c++  java
  • iOS开发UI篇—核心动画(转场动画和组动画)

    所谓的动画组就是将一些动画组合起来给layer使其的动画更丰富灵活。
    它很简单,就是为其animations属性赋值一个动画数组。
     
    [cpp] view plaincopy
     
    1. - (void)demoAnimationGroup  
    2. {  
    3.     static NSString * const kCAPostionKeyPath = @"position";  
    4.     static NSString * const kCAOpacityKeyPath = @"opacity";  
    5.     static NSString * const kCARotationKeyPath = @"transform.rotation";  
    6.     static NSString * const kCAScaleKeyPath = @"transform.scale";  
    7.       
    8.     UIBezierPath *path = [UIBezierPath bezierPath];  
    9.     [path moveToPoint:_demoView.layer.position];  
    10.     [path addCurveToPoint:CGPointMake(260, 400) controlPoint1:CGPointMake(0, 460) controlPoint2:CGPointMake(320, 0)];  
    11.       
    12.     //路径动画  
    13.     CAKeyframeAnimation *posAnimation = [CAKeyframeAnimation animationWithKeyPath:kCAPostionKeyPath];  
    14.     posAnimation.path = path.CGPath;  
    15.     posAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
    16.       
    17.     //透明度动画  
    18.     CABasicAnimation *opaAnimation = [CABasicAnimation animationWithKeyPath:kCAOpacityKeyPath];  
    19.     opaAnimation.duration = 2.0f;  
    20.     opaAnimation.toValue = @(0.3f);  
    21.     opaAnimation.autoreverses = YES;  
    22.       
    23.     //旋转动画  
    24.     CABasicAnimation *rotAnimation = [CABasicAnimation animationWithKeyPath:kCARotationKeyPath];  
    25.     rotAnimation.duration = 2.0f;  
    26.     rotAnimation.toValue = @(M_PI);  
    27.     rotAnimation.autoreverses = YES;  
    28.       
    29.     //缩放动画  
    30.     CABasicAnimation *scaAnmaiton = [CABasicAnimation animationWithKeyPath:kCAScaleKeyPath];  
    31.     scaAnmaiton.duration = 2.0f;  
    32.     scaAnmaiton.toValue = @(1.5f);  
    33.     scaAnmaiton.autoreverses = YES;  
    34.       
    35.     //设置动画组  
    36.     CAAnimationGroup *group = [CAAnimationGroup animation];  
    37.     group.animations = @[posAnimation, opaAnimation, rotAnimation, scaAnmaiton];  
    38.     group.duration = 4.0f;  
    39.     group.removedOnCompletion = NO;  
    40.     group.fillMode = kCAFillModeForwards;  
    41.       
    42.     [_demoView.layer addAnimation:group forKey:nil];  
    43. }  

    这个动画组对demoView的layer加入了一些同时进行的动画,比如双控制点的贝塞尔路径,透明度的渐隐渐明,旋转以及缩放等。
     
     

    CATransition

     
    转场动画几乎在所有的APP上都能遇见,经常用于视图控制器的切换或者单视图控制器的页面切换等,也可以在自定义UIStoryboardSegue中来加入动画效果。
     
    它也是CAAnimation的子类,能为图层提供移出屏幕和移入的动画效果。
    其常用属性为
     
    type 过渡类型
    subtype 过渡类型的子类型,方向设置
     
    其中过渡类型有很多,最初有四个版本,后来又加入了一些,以字符串形式设置。
     
    最初的四个:fade,push,moveIn,reveal
    以后加入的效果则更加丰富和炫目,有 cube, oglFlip, suckEffect, rippleEffect, pageCurl, pageUnCurl, cameraIrisHollowOpen, cameraIrisHollowClose。
     
    [cpp] view plaincopy
     
    1. - (void)viewTransition  
    2. {  
    3.     static NSString * const kCATransitionTypeFlip = @"oglFlip";     //翻页效果  
    4.       
    5.     CATransition *transition = [CATransition animation];  
    6.     transition.type = kCATransitionTypeFlip;  
    7.     transition.subtype = kCATransitionFromRight;  
    8.     transition.duration = 1.5f;  
    9.     [_transitionOrangeView.layer addAnimation:transition forKey:nil];  
    10.       
    11.     [self.view performSelector:@selector(sendSubviewToBack:) withObject:_transitionOrangeView afterDelay:1.0f];  
    12. }  

    这个方法简单的实现了view翻页的效果,当然目前控制器的根view只有一个subview,所以使用sendSubviewToBack的方法后显示的还是这个view,但是看到了动画效果。
     
    现在UIView的转场动画有了更方便的类方法
    + transitionWithView:duration:options:animations:completion:
    + transitonFromView:toView:duration:options:completion:
     
    这两个方法参数加入了苹果推荐的块代码,也让转场动画都在一个方法中进行处理,也对动画结束进行了处理,更加方便易用。
     
    [cpp] view plaincopy
     
    1. - (void)anotherTransition  
    2. {  
    3.     _transitionBlueView = [[UIView alloc] initWithFrame:self.view.bounds];  
    4.     _transitionBlueView.backgroundColor = [UIColor blueColor];  
    5.       
    6.     [UIView transitionFromView:_transitionOrangeView  
    7.                         toView:_transitionBlueView  
    8.                       duration:1.0f  
    9.                        options:UIViewAnimationOptionTransitionCrossDissolve     
    10.                     completion:nil];  
    11. }  

    代码很简洁和易读。
    不过要注意的一点是,这里的参数并不是很多,而且我并没有对蓝色视图通过addSubview加载到self.view中,也没有对橘色视图进行removeFromSuperview,这些方法都封装在这个类方法中隐式进行了。
     
     
  • 相关阅读:
    mysql优化技巧
    ffmpeg3.3.2命令行参数笔记
    redis性能指标
    linux下ifconfig只剩下lo的解决方法
    symfony3 yml配置文件详解
    pt工具校验主从数据一致性之dsns方式
    K8S(01)二进制部署实践-1.15.5
    K8s之配置文件kube config生成
    [转]CA认证原理及CFSSL证书生成工具的使用
    K8s集成实战-使用spinnaker进行自动化部署
  • 原文地址:https://www.cnblogs.com/iosblogx/p/4474358.html
Copyright © 2011-2022 走看看