2017-06-26 8 views
5

私は追加して、子ビューコントローラを削除し、次の2つの機能は、コンテナビューコントローラからトリガがあります。コンテナビューコントローラに追加/削除するときに子ビューコントローラをアニメーション化するにはどうすればよいですか?

@discardableResult func addChildViewController(withChildViewController childViewController: UIViewController) -> UIViewController { 
    // Add Child View Controller 
    addChildViewController(childViewController) 
    childViewController.beginAppearanceTransition(true, animated: true) 
    // Add Child View as Subview 
    view.addSubview(childViewController.view) 
    // Configure Child View 
    childViewController.view.frame = view.bounds 
    childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    // Notify Child View Controller 
    childViewController.didMove(toParentViewController: self) 
    return childViewController 
} 
@discardableResult func removeChildViewController(withChildViewController childViewController: UIViewController) -> UIViewController { 
    // Notify Child View Controller 
    childViewController.willMove(toParentViewController: nil) 
    childViewController.beginAppearanceTransition(false, animated: true) 
    // Remove Child View From Superview 
    childViewController.view.removeFromSuperview() 
    // Notify Child View Controller 
    childViewController.removeFromParentViewController() 
    return childViewController 
} 

上記の機能はのUIViewControllerを拡張するものであるので、私はやっているすべてはself.addChildViewController(ある)と親ビューコントローラのself.removeChildViewController()

ビューの途中で削除されているビューをアニメーション化し、ビューを追加するにはどうすればよいですか?別の子ビューコントローラ間

+0

複数の子コントローラがあり、スイッチを入れようとしていますか、1台だけですか? –

+0

私は複数の子ビューコントローラを持っていますが、いつでも私は1つを追加して最後のものを削除します。 –

+0

子ビューコントローラを追加したときに、どのアニメーションを使用しますか? –

答えて

3

のアニメーション: - 上記で

func cycleFromViewController(oldViewController: UIViewController, toViewController newViewController: UIViewController) { 
    oldViewController.willMove(toParentViewController: nil) 
    newViewController.view.translatesAutoresizingMaskIntoConstraints = false 

    self.addChildViewController(newViewController) 
    self.addSubview(subView: newViewController.view, toView:self.containerView!) 

    newViewController.view.alpha = 0 
    newViewController.view.layoutIfNeeded() 

    UIView.animate(withDuration: 0.5, delay: 0.1, options: .transitionFlipFromLeft, animations: { 
     newViewController.view.alpha = 1 
     oldViewController.view.alpha = 0 
    }) { (finished) in 
     oldViewController.view.removeFromSuperview() 
     oldViewController.removeFromParentViewController() 
     newViewController.didMove(toParentViewController: self) 
    } 
} 

  • oldViewController: - 現在は子供のViewControllerを表示
  • newViewController: - 追加しようとしてます、新しい子ビューコントローラ
  • containerView: - すべての子コントローラが表示されているビューです。

子ビューをアニメーション化するには、要件に従って利用できるUIViewAnimationOptionstransitionFlipFromLeftを交換することにより、アニメーションのスタイルの異なるタイプを使用することができます。

+0

これは私をいくらか近くにしています。私はview.addSubview(newViewController.view)をしなければならなかった:self.addSubview(subView:newViewController.view、toView:self.containerView!)はうまくいかなかった。しかし、私の見解が消えて初めて、消えてしまいましたが、その後は私の意見は決して示されません。 –

関連する問題