2017-08-07 105 views
1

我想實現在這裏定製的過渡動畫是我自定義的UINavigationController動畫

class NavDelegate: NSObject, UINavigationControllerDelegate { 
    private let animator = Animator() 

    func navigationController(_ navigationController: UINavigationController, 
           animationControllerFor operation: UINavigationControllerOperation, 
           from fromVC: UIViewController, 
           to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return animator 
    } 
} 

class Animator: NSObject, UIViewControllerAnimatedTransitioning { 
    func transitionDuration(using context: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return 10.0 
    } 

    func animateTransition(using context: UIViewControllerContextTransitioning) { 
     let fromViewController = context.viewController(forKey: UITransitionContextViewControllerKey.from)! 
     let toViewController = context.viewController(forKey: UITransitionContextViewControllerKey.to)! 
     if fromViewController is ViewController { 
      self.pushAnimation(from: fromViewController as! ViewController, 
           to: toViewController as! VC2ViewController, 
           with: context) 
     } 

     if toViewController is ViewController { 
      print("pop") 
     } 

    } 

    func pushAnimation(from viewController: ViewController, 
         to destinationViewController: VC2ViewController, 
         with context: UIViewControllerContextTransitioning) { 

     context.containerView.addSubview(destinationViewController.view) 


     //block 1 
     for cell in viewController.tableView1.visibleCells { 
      if let castCell = cell as? VC1TableViewCell { 
       castCell.contentViewToLeft.constant = -UIScreen.main.bounds.width 
       castCell.contentViewToRight.constant = UIScreen.main.bounds.width 
       let duration = Double(viewController.tableView1.visibleCells.index(of: cell)!)/Double(viewController.tableView1.visibleCells.count) + 0.2 
       UIView.animate(withDuration: duration, animations: { 
        castCell.layoutIfNeeded() 
       }) { animated in 
       } 
      } 
     } 

     //block 2 
     for cell in destinationViewController.tableView2.visibleCells { 
      if let castCell = cell as? VC2TableViewCell { 
       castCell.contentViewToLeft.constant = -UIScreen.main.bounds.width 
       castCell.contentViewToRight.constant = UIScreen.main.bounds.width 
       let duration = Double(destinationViewController.tableView2.visibleCells.index(of: cell)!)/Double(destinationViewController.tableView2.visibleCells.count) + 0.2 
       UIView.animate(withDuration: duration, animations: { 
        castCell.layoutIfNeeded() 
       }) { animated in 
        if duration > 1.1 { 
         context.completeTransition(!context.transitionWasCancelled) 
        } 
       } 
      } 
     } 
    } 
} 

的問題是目標控制器的約束該動畫(塊2)從未動畫,layoutIfNeeded()執行沒有動畫,雖然塊1正在工作。可能是什麼問題?

回答

0

我花了幾天的測試

transitionDuration(using context: UIViewControllerContextTransitioning?)

功能,並發現它是所有關於調用內部DispatchQueue.main.async {}toViewController動畫一些塊。不知道它是如何工作的,但我讓我的代碼按照我的計劃工作。

相關問題