这是我在 Swift 中的实现:
func navigateTo(vc: NSViewController){
vc.view.frame = self.containerView.bounds;
if let currentSubview = containerView.subviews.first {
self.containerView.replaceSubview(currentSubview, with: vc.view)
} else {
self.containerView.addSubview(vc.view)
}
self.addChildViewController( vc )
}
对于动画过渡,我实现了 awakeFromNib
override func awakeFromNib() {
let animation: CATransition = CATransition()
animation.type = kCATransitionMoveIn
animation.subtype = kCATransitionFromRight
animation.duration = 0.75
animation.delegate = self
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
if let _ = containerView {
containerView.wantsLayer = true
containerView.animations = NSDictionary(object: animation, forKey: "subviews") as! [String : AnyObject]
}
}
并将self.containerView.replaceSubview行替换为:
self.containerView.animator().replaceSubview(currentSubview, with: vc.view)