美文网首页
iOS 自定义转场动画。

iOS 自定义转场动画。

作者: ZCY_YAM | 来源:发表于2017-01-09 12:03 被阅读44次

    以前对iOS 的专场动画一直不敢去动自定义动画(非交互式) 怕自己搞不好,最近无事就突突一下。发现没有想象的那么难!(大神就莫见笑。纯属自我学习
    储备只是 简单UIView动画。仅此而已,
    能看懂 代理 协议神马的。

    首先我们以PresentViewController 为例简单说一下 苹果自己定义了几种动画效果这里就不说了;上代码! 创建一个Button

    - (IBAction)presentaction:(id)sender { UIViewController * vc = [[UIViewController alloc]init]; [vc.view setBackgroundColor:[UIColor yellowColor]]; 
    //将呈现样式选为定值型 自己定义
     [vc setModalPresentationStyle:UIModalPresentationCustom]; 
    //过度代理设为自己,这里是要点!一定要制定一个代理是它的过渡动画走它的代理。没用过的同学可以进去看看代理方法 
    [vc setTransitioningDelegate:self]; NSLog(@"%s\nvc.view:%@\nself.view:%@\nself.navigationController.view:%@",__func__,vc.view,self.view,self.navigationController.view);
     [self.navigationController presentViewController:vc animated:YES completion:^{ for(UIView * v in self.navigationController.view.subviews){ NSLog(@"%@",v); } }];
    }
    

    上面之所以打很多log 是为了想弄明白一个

     UIView * containerView = [transitionContext containerView];
    

    这个view 是个神马鬼。下面再说
    看完UIViewControllerTransitioningDelegate的同学是不是发现点什么。非交互转场动画就看前两个就ok 交互式下次再说。

    /*开始弹出时走的方法。 返回一个实现UIViewControllerAnimatedTransitioning的对象*/
    - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
    /*消失时走的方法。 返回一个实现UIViewControllerAnimatedTransitioning的对象*/
    - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
    

    从上面可以看出不管是呈现还是消失都会返回一个实现UIViewControllerAnimatedTransitioning的对象。所以呢 我们需要创建一个对象实现UIViewControllerAnimatedTransitioning这玩意儿 点协议进去后你发现只有3个方法!两个必须实现的!

    - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;@optional- (void)animationEnded:(BOOL) transitionCompleted;
    

    管它什么鬼先实现再说

    - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{ 
    return 1;
    }
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{}
    

    第一个方法没啥说的动画时间。关键看第二个方法如下

    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
     UIView * containerView = [transitionContext containerView];
    //暂时我理解为一个容器View 动画在它内实现 如有错误请留言打脸!小弟多谢! 
    NSLog(@"containerView:%@",containerView); UIViewController * fromeVC = 
    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    //字面意思来自哪个VC UIViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    //字面意思将呈现哪个VC 
    NSLog(@"fromeVC.view:%@\ntoVC.view:%@",fromeVC.view,toVC.view); 
    [toVC.view setFrame:containerView.frame]; toVC.view.layer.anchorPoint = 
    CGPointMake(0.5, 1); toVC.view.layer.position = CGPointMake(containerView.center.x, containerView.frame.size.height); fromeVC.view.layer.position = CGPointMake(containerView.center.x, containerView.frame.size.height); 
    fromeVC.view.layer.anchorPoint = CGPointMake(0.5, 1);
     [containerView addSubview:toVC.view];
     /*动画自己*/
     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ 
    [fromeVC.view setFrame:CGRectMake(20, 20, containerView.bounds.size.width-40, containerView.frame.size.height-10)]; 
    toVC.view.transform = CATransform3DGetAffineTransform(CATransform3DMakeRotation(-0.5,0.3,0,0)); 
    } completion:^(BOOL finished) { 
    /*结束过渡*/
     [transitionContext completeTransition:YES];
     }];
    

    然后回到刚vc 的

    - (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0)
    { 
        vcAnimation * animation = [[vcAnimation alloc]init]; return animation;
    }
    

    相关文章

      网友评论

          本文标题:iOS 自定义转场动画。

          本文链接:https://www.haomeiwen.com/subject/kfthbttx.html