• iOS转场动画初探


    一般我们就用两种转场push和present

    present

    /**
     1.设置代理
     - (instancetype)init
     {
     self = [super init];
     if (self) {
     self.transitioningDelegate = self;
     self.modalPresentationStyle = UIModalPresentationCustom;
     }
     return self;
     }
     2.添加协议<UIViewControllerTransitioningDelegate>
     3.重写协议方法 - 在协议方法里面初始化
     - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
     
     }
     
     - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
     
     }
     **/

    2.push

    第一个控制器推第二个控制器的时候

       ViewController2 *vc = [[ViewController2 alloc]init];
        self.navigationController.delegate = vc; //必须写这句话
        [self.navigationController pushViewController:vc animated:YES];

    第二个控制器

    @interface ViewController2 : UIViewController<UINavigationControllerDelegate>
    
    @end

    代理方法

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

    上面就是push和present发生的地方,都需要返回一个 UIViewControllerAnimatedTransitioning

    所以我们写一个类

    @interface WJTransiation_Mobile : NSObject<UIViewControllerAnimatedTransitioning>

    @end

    然后修改里面的协议方法

    //动画时间

    - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;

    //自定义过渡动画

    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

    执行过渡动画

    1.获取前后控制器

       UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    2.获得转场动画视图

    UIView *containerView = [transitionContext containerView];

    3.将前后控制器需要进行动画的视图添加到containerView上

       [containerView addSubview:tempView];
        [containerView addSubview:toVC.view];

    4.执行自己的动画

    demo链接:http://pan.baidu.com/s/1kTPPUMb

    效果图:

    补:

    //动态添加transiationView属性
    
    @interface UIViewController (transiationView)
    
    @property (nonatomic,strong)UIView *transiationView;
    
    @end
    #import <objc/runtime.h>
    
    @implementation UIViewController (transiationView)
    
    static char transiationViewKey;
    
    - (void)setTransiationView:(UIView *)transiationView {
        return objc_setAssociatedObject(self, &transiationViewKey, transiationView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIView *)transiationView {
         return objc_getAssociatedObject(self, &transiationViewKey);
    }
    
    @end
  • 相关阅读:
    20190710-汉诺塔算法
    20190705-Python数据驱动之DDT
    20190621-N皇后
    还在为Excel合并单元格导致的各种问题烦恼吗?这里一起解决
    Excel基础:开始菜单之对齐方式,那些被遗忘的实用功能
    Excel中身份证号码如何分段显示,难倒小编,有什么好方法吗?
    制作这样的Excel注水图表,让老板另眼相看,坐等升职加薪
    Excel高手都会的Shift快捷键7个用法,让工作效率翻倍
    Excel答粉丝问:批量将单元格内容转为批注
    Excel基础:开始菜单之字体的华丽转身
  • 原文地址:https://www.cnblogs.com/hxwj/p/5069806.html
Copyright © 2020-2023  润新知