zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    最长公共子序列解题报告
    数列操作问题
    数字金字塔解题报告
    Formiko总结整数十进制转换二进制原理
    程序设计竞赛问题类型
    vue 的生命周期
    小程序 瀑布流布局(图-视频)
    JavaScript语言里判断一个整数是偶数还是奇数,并输出判断结果
    JavaScript语言里判断一个整数,属于哪个范围:大于0;小于0;等于0
    test
  • 原文地址:https://www.cnblogs.com/hxwj/p/5069806.html
Copyright © 2011-2022 走看看