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中使用它一次。 –