UIViewControllerAnimatedTransitioning只能在其他时间运行?

问题描述:

我一直在这个小时,不能理解我做错了什么。我已经按照我之前的Swift: Problems with custom UIView.transition?UIViewControllerAnimatedTransitioning只能在其他时间运行?

所述符合UITabBarControllerDelegate来装配自定义标签栏控制器转换。我没有使用正常情节串联板标签栏按钮,我通过编程方式切换了selectedIndex。我的问题是,只有这样实现:

func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     let animator = ModalTransition() 
     animator.fromView = fromVC.view 
     animator.toView = toVC.view 
     return animator 

    } 

指数的动画和切换只发生在隔一段时间。我有自定义按钮来切换索引和其他时间,当我单击切换按钮时没有任何反应。这是我的动画:

// 
// ModalTransition.swift 
// Adventures In Design 
// 
// Created by Skylar Thomas on 8/28/17. 
// 

import UIKit 

class ModalTransition: NSObject, UIViewControllerAnimatedTransitioning { 

    weak var transitionContext: UIViewControllerContextTransitioning? 
    var fromView = UIView() 
    var toView = UIView() 
    var duration = 1.1 


    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return 1 
    } 

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 

     let containerView = transitionContext.containerView 
     containerView.addSubview(toView) 
     containerView.sendSubview(toBack: toView) 

     print("ANIMATING") 

     UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: { 
      self.fromView.center.y += 900 

     }, completion: { 
      finished in 

      //only works every OTHER click 
      transitionContext.completeTransition(!transitionContext.transitionWasCancelled) 
      self.fromView.center.y -= 900 
     }) 


    } 
} 

这是什么原因造成的?这是什么

我认为你不是分配你的toView和FromView。尝试类似这样的

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
      let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)! 
      let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)! 

      let fromView = fromViewController.view 
      let toView = toViewController.view 
      let container = transitionContext.containerView 
      container.addSubview(toView!) 
      // Replace your animations here 
toView?.frame = transitionContext.finalFrame(for: toViewController) 
     toView?.alpha = 0 
     let duration = self.transitionDuration(using: transitionContext) 
     UIView.animate(withDuration: duration, delay: 0, options: .curveEaseInOut, animations: { 
      toView?.alpha = 1 
      fromView?.alpha = 0 
     }, completion: { finished in 
      toView?.alpha = 1.0 
      fromView?.alpha = 1 
      fromView?.removeFromSuperview() 
      transitionContext.completeTransition(true) 

     }) 

     } 
+0

仍然只适用于所有其他人。你能展示一个动画例子吗?也许我没有正确动画 – skyguy

+0

当然..检查编辑答案。让我知道,如果它有帮助 –

+0

在那里的东西修复它。谢谢! – skyguy