zoukankan      html  css  js  c++  java
  • iOS动画

    一、CABasicAnimation实现View动画

    //tarnsform放大缩小动画
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//根据传的keypath实现不同动画
    
    animation.duration = 0.3f;
    
    animation.repeatCount = 0;
    
    animation.autoreverses = YES;
    
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    
    animation.toValue = [NSNumber numberWithFloat:1.1f];
    
    animation.byValue = [NSNumber numberWithFloat:0.9f];
    
    animation.removedOnCompletion = YES;
    
    [view.layer addAnimation:animation forKey:@"animation_key"];

    keypath值:transform.scale = 大小比例

    transform.scale.x = 宽的比例转换

    transform.scale.y = 高的比例转换

    transform.rotation.z = 平面的旋转

    opacity = 透明度

    margin = 布局

    zPosition = 翻转

    backgroundColor = 背景颜色

    cornerRadius = 圆角

    borderWidth = 边框宽

    bounds = 大小

    contents = 内容

    contentsRect = 内容大小

    cornerRadius = 圆角

    frame = 大小位置

    hidden = 显示隐藏

    mask

    masksToBounds

    opacity

    position

    shadowColor

    shadowOffset

    shadowOpacity

    shadowRadius

    二、View之间的动画效果

      参考http://www.cocoachina.com/ios/20141226/10775.html

    三、控制器间的过渡动画  

    MyViewController *myVC = [[MyViewController alloc]init];
     //创建动画
     CATransition *animation = [CATransition animation];
     //设置运动轨迹的速度
     animation.timingFunction = UIViewAnimationCurveEaseInOut;
     //设置动画类型为立方体动画
     animation.type = @"cube";
     //设置动画时长
     animation.duration =0.5f;
     //设置运动的方向
     animation.subtype =kCATransitionFromRight;
     //控制器间跳转动画
     [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];
    [self presentViewController:myVC animated:NO completion:nil];

    设置动画类型可以为:

      Fade = 1,                   //淡入淡出

        Push,                       //推挤

        Reveal,                     //揭开

        MoveIn,                     //覆盖

        Cube,                       //立方体

        SuckEffect,                 //吮吸

        OglFlip,                    //翻转

        RippleEffect,               //波纹

        PageCurl,                   //翻页

        PageUnCurl,                 //反翻页

        CameraIrisHollowOpen,       //开镜头

        CameraIrisHollowClose,      //关镜头

    当需要上下翻页或左右翻转动画时候,可以使用一下方法:

    
    
    MyViewController *myVC = [[MyViewController alloc]init];

    [UIView beginAnimations:nil context:NULL];
    //置动画块中的动画属性变化的曲线  
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //设置持续时间
    [UIView setAnimationDuration:0.5];
    //设置过渡的动画效果
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[UIApplication sharedApplication].keyWindow cache:NO];
    [self presentViewController:
    myVC animated:NO completion:nil];
    //提交动画 [UIView commitAnimations];
    过渡的动画效果:

       UIViewAnimationTransitionFlipFromLeft,

          UIViewAnimationTransitionFlipFromRight,

          UIViewAnimationTransitionCurlUp,

          UIViewAnimationTransitionCurlDown,

    四、自定义控制器跳转动画

    1.原理

    当我们进行视图切换的时候,无论是push,pop还是通过PresentModal和dismiss时,我们可以把跳转动画分割为三个部分,
    1:第一个视图控制器显示。
    2:临时视图控制器 显示动画效果(并不是真的存在这个控制器,其实只有一个containerView)
    3:显示第二个视图控制器。
    用形象一点的说法,我们可以将第二部分也看做是一个临时的视图控制器,他执行一段关于UIView的动画,我们只要让动画的开始界面与控制器1一样,结束界面与视图控制器2一样。当动画执行结束,执行到第三步时,把临时的视图控制器释放。这样只要稍微实现一些UIView,CALayer的动画。

    2.用到的协议及方法

    @protocol UIViewControllerAnimatedTransitioning

    实现这个协议的对象就相当于是之前说到的临时视图控制器,协议中主要用到的两个方法是:

    //这个方法中我们只要返回页面跳转动画的执行时间 
    -(NSTimeInterval)transitionDuration:(id < UIViewControllerContextTransitioning >)transitionContext;
    //在这个方法中实现具体动画
    -(void)animateTransition:(id < UIViewControllerContextTransitioning >)transitionContext;

    第二个方法是跳转动画核心内容,方法中的参数transitionContext是当前临时控制器界面的上下文。通过上下文,我们先拿到临时视图控制器的背景视图containerView,他已经有一个子视图,即fromViewController.view。我们要做的是,先获得toViewController,然后把当前视图加到内容视图上

    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [[transitionContext containerView] addSubview:toVC.view];

    剩下的部分就可以发挥我们的系统核心动画的学习水平,比如修改toVC.view的frame,从下方旋转弹出等等。

    @protocol UINavigationControllerDelegate

    这是修改Navigation跳转动画需要实现的协议,当我们通过pop或者push进行跳转时,都会调用下面的方法(协议的其他方法由于暂时用不到,所以就不介绍了),我们可以看到返回值是id <UIViewControllerAnimatedTransitioning>,就是我们之前编写的动画对象。

    - (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC;

    方法中的operation参数是一个枚举类型,我们可以判断它的值是 UINavigationControllerOperationPush还是
    UINavigationControllerOperationPop来区别当前的跳转方式。

    @protocol UIViewControllerTransitioningDelegate

    这是修改模态跳转动画需要实现的协议,我们进行跳转PresentModal和dismiss时会分别调用两个方法:

    //PresentModal跳转回调方法 
    - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
     //dismiss跳转回调方法 
    - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;

    这两个方法的返回值和上面navigation回调方法的返回值一样,我们只需要初始化我们的实现了UIViewControllerTransitioningDelegate协议的动画对象就可以了。具体的实现以及效果可以查看文章末尾附上的Demo。

    navigation跳转需要注意的地方

    在使用Navigation跳转的时候,我们希望视图1跳转到视图2和视图2返回视图1有自定义动画时,我们只用在视图1中将设置视图1的navigation的delagate。其次delegate的设置最好放在viewWillAppear中。

    demo所实现的最终效果只出现在点击按钮时的跳转动画,而不包括像navigation侧滑返回那样的手势动画。如何怕侧滑效果不统一,我们可以先把侧滑返回的效果关闭:

    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    可以参考一个半圆跳转动画效果

  • 相关阅读:
    熟悉常用的Linux操作
    Python基础之五星红旗
    类似于铁道部12306的城市选择框的实现
    使用Django操作数据库入门
    进程和线程
    线程、进程、携程理解
    CentOS6.8部署Python3.6.8的Django项目
    爬取妹子图片
    聚类算法之DBSCAN
    机器学习算法优缺点总结
  • 原文地址:https://www.cnblogs.com/liuluoxing/p/6852605.html
Copyright © 2011-2022 走看看