2017-06-26 20 views
5

Tôi có 2 chức năng sau đó thêm và loại bỏ các bộ điều khiển xem đứa trẻ được kích hoạt từ một bộ điều khiển xem container:Làm cách nào để animate bộ điều khiển chế độ xem con khi thêm vào/xóa khỏi bộ điều khiển chế độ xem bộ chứa?

@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 
} 

Các chức năng trên là phần mở rộng để UIViewController, vì vậy tất cả tôi đang làm là self.addChildViewController() và self.removeChildViewController() trên bộ điều khiển xem cha.

Làm cách nào để tạo hiệu ứng an toàn cho chế độ xem đang được xóa khi đang xem và chế độ xem đang được thêm vào theo cách của nó?

+0

Bạn có nhiều bộ điều khiển con và cố gắng chuyển đổi trong betweens Hoặc chỉ một duy nhất? –

+0

Tôi có nhiều bộ điều khiển chế độ xem con, nhưng tại bất kỳ thời điểm nào, tôi sẽ thêm một bộ điều khiển và xóa bộ điều khiển cuối cùng. –

+0

Bạn muốn hình động nào khi bộ điều khiển xem con thêm? –

Trả lời

3

Animating giữa các bộ điều khiển xem đứa trẻ khác nhau: -

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) 
    } 
} 

Trong trên,

  • oldViewController: - Current hiển thị con viewController
  • newViewController: - Mới nhìn con điều khiển mà sẽ sẽ thêm
  • containerXem: - Chế độ xem trong đó tất cả các trình điều khiển con đang hiển thị.

Để động xem đứa trẻ, bạn có thể sử dụng loại khác nhau của phong cách hoạt hình bằng cách thay thế transitionFlipFromLeft để sẵn UIViewAnimationOptions theo yêu cầu.

+0

Điều này khiến tôi hơi gần. Tôi đã phải làm view.addSubview (newViewController.view): self.addSubview (subView: newViewController.view, toView: self.containerView!) Không hoạt động. Với nó, mặc dù, lần đầu tiên quan điểm của tôi biến mất, nó biến mất, nhưng sau đó sau đó, quan điểm của tôi không bao giờ hiển thị. –

Các vấn đề liên quan