自定义viewController切换动画

实现效果

系统自带的viewControll切换动画有push动画和模态动画,一般在正常情况下已经够用了,但并满足不了一些复杂的切换效果,比如下图一些效果github代码链接

image

基本介绍

相关的内容都定义在UIKit的UIViewControllerTransitioning.h中

  • 设置动画时间
   func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return animationDuration
    }
  • 在这里实现自定义动画
     func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//动画之前的视图
                self.fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
                //即将出现的视图
                self.toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)//containerView是容器的view,需要将要出现的view加载上去
                transitionContext.containerView()?.addSubview(self.toVC!.view)
                //添加手势到即将出现的view上
                self.prepareGestureRecognizerInView(toVC!.view)
                //对动画之前的视图进行调整,比如frame
                self.pushBefore()
                //自定义动画效果
        UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                self.popAnimation()
            }
            }) { (isCompelete) -> Void in
                if self.isPush
                {
                //完成时调用
                    transitionContext.completeTransition(true)
                }else{
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled());
                }
        }
}
  • 需要实现的代理方法

    //返回Presented动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.animation?.isPush = true
        return self.animation
    }
    
    //返回Dismissed动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.animation?.isPush = false
        return self.animation
    }
    
    //返回push和pop动画
    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == UINavigationControllerOperation.Push{
            self.animation!.isPush = true
        }else{
            self.animation!.isPush = false
        }
        return self.animation
    }

    具体实现请看代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值