1
我正在使用分段控件在两个不同视图之间切换。 但是,通过我的代码,每次显示视图时都会重新载入视图。分段控制无需重新加载
这里是我的代码:
let newController = storyboard?.instantiateViewControllerWithIdentifier(viewControllerIdentifiers[sender.selectedSegmentIndex]) as! userProfileViewController
let oldController = childViewControllers.last as! UIViewController
newController.userToShow = self.userToShow
oldController.willMoveToParentViewController(nil)
addChildViewController(newController)
newController.view.frame = oldController.view.frame
transitionFromViewController(oldController, toViewController: newController, duration: 0.25, options: .TransitionCrossDissolve, animations:{() -> Void in
// nothing needed here
}, completion: { (finished) -> Void in
oldController.removeFromParentViewController()
newController.didMoveToParentViewController(self)
})
} else if sender.selectedSegmentIndex == 1 {
let newController = storyboard?.instantiateViewControllerWithIdentifier(viewControllerIdentifiers[sender.selectedSegmentIndex]) as! secondProfilePageViewController
let oldController = childViewControllers.last as! UIViewController
newController.userToShow = self.userToShow
oldController.willMoveToParentViewController(nil)
addChildViewController(newController)
newController.view.frame = oldController.view.frame
transitionFromViewController(oldController, toViewController: newController, duration: 0.25, options: .TransitionCrossDissolve, animations:{() -> Void in
// nothing needed here
}, completion: { (finished) -> Void in
oldController.removeFromParentViewController()
newController.didMoveToParentViewController(self)
})
}
}
我还试图通过使用两个UIContainerViews,并隐藏一个我并不需要这样做,但这种方式我不能有任何的动画在意见之间通过。
为了更好地理解,我想在每个Iphone中做一些类似Today/Notifications的东西。
但是,我怎样才能让段落变成动画,而不是隐藏我不需要的视图? – dpstart
我认为问题在于你总是得到一个新的视角,而不是它没有动画。 – Mundi
@dpstart你已经使用容器视图控制器,就像他提到的那样,它是运行你的动画代码的VC。你只需要存储你想要在VC上交换的两个VC的引用。我会在viewDidLoad中创建两个VC,并只跟踪哪一个可见。你的实例化行实际上是创建一个新的VC,所以你只需要在每个你想要交换的VC中使用它一次。 –