joh*_*yan 11 iphone animation objective-c ios ios10
我注意到ios 10中条形色调动画的方式发生了变化.我创建了一个概述更改的示例项目:Github:ios10BarTintDemo
基本上在ios 9上,barTintColor可以平滑地使用 [UIViewControllerTransitionCoordinator animateAlongsideTransition]
但是在ios 10上,动画不太平滑,当弹出一个视图控制器根本没有动画时,我尝试[self.navigationController.navigationBar layoutIfNeeded]
在一些类似的答案中添加,但这在推/控制器时似乎没有任何效果.
kam*_*soc 15
我已经在iOS 10.3中测试过,我认为这个问题已得到修复.而且transitionCordinator
不再需要了.我觉得动画很流畅.请检查我在github上的项目或查看此代码:
class ViewControllerA: UIViewController {
override func loadView() {
super.loadView()
title = "A"
view.backgroundColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(self.showController))
}
override func viewWillAppear(_ animated: Bool) {
setColors()
super.viewWillAppear(animated)
}
func showController() {
navigationController?.pushViewController(ViewControllerB(), animated: true)
}
private func setColors() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .red
navigationController?.navigationBar.isTranslucent = false
}
}
class ViewControllerB: UIViewController {
override func loadView() {
super.loadView()
title = "B"
view.backgroundColor = .white
}
override func viewWillAppear(_ animated: Bool) {
setColors()
super.viewWillAppear(animated)
}
override func willMove(toParentViewController parent: UIViewController?) {
if parent == nil {
navigationController?.navigationBar.barTintColor = .red
}
super.willMove(toParentViewController: parent)
}
private func setColors() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.isTranslucent = false
}
}
Run Code Online (Sandbox Code Playgroud)
================================================== ================================================== ================================================== ================================================== ================================================== ==================================================
为了实现这种动画,你应该使用的UIViewControllerTransitionCoordinator
是苹果的文件说,这是:
采用UIViewControllerTransitionCoordinator协议的对象为与视图控制器转换关联的动画提供支持.(...)
所以每个人UIViewController
都拥有transitionController
.要得到这个,你应该打电话给UIViewControllerClass
:
self.transitionCoordinator()
来自文档:
返回活动的转换协调器对象.
因此,要获得您想要的结果,您应该animateAlongsideTransition
在viewController transitionCoordinatior中实现方法.单击backButton
并向后滑动即可使用动画.
示例:
第一控制员:
class ViewControllerA: UIViewController {
override func loadView() {
super.loadView()
title = "A"
view.backgroundColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(self.showController))
setColors()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animate()
}
func showController() {
navigationController?.pushViewController(ViewControllerB(), animated: true)
}
private func animate() {
guard let coordinator = self.transitionCoordinator else {
return
}
coordinator.animate(alongsideTransition: {
[weak self] context in
self?.setColors()
}, completion: nil)
}
private func setColors() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .red
}
}
Run Code Online (Sandbox Code Playgroud)
第二控制器:
class ViewControllerB : UIViewController {
override func loadView() {
super.loadView()
title = "B"
view.backgroundColor = .white
setColors()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animate()
}
override func willMove(toParentViewController parent: UIViewController?) { // tricky part in iOS 10
navigationController?.navigationBar.barTintColor = .red //previous color
super.willMove(toParentViewController: parent)
}
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = .blue
}
private func animate() {
guard let coordinator = self.transitionCoordinator else {
return
}
coordinator.animate(alongsideTransition: {
[weak self] context in
self?.setColors()
}, completion: nil)
}
private func setColors(){
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.barTintColor = .blue
}
}
Run Code Online (Sandbox Code Playgroud)
更新iOS 10
在iOS 10中,棘手的部分是willMoveTo(parentViewController parent: UIViewController?)
在第二个 ViewController中添加.并将navigationBar设置tintColor
为上一个控制器的颜色值.另外,在第二个 ViewControler 中的viewDidAppear
方法中,设置第二个 viewController 的颜色.navigationBar.tintColor
小智 5
您可以通过添加类似于此的内容来修复此弹出问题,在viewWillDisappear中运行它将因iOS10中的某些原因而无法正常工作
override func willMove(toParentViewController parent: UIViewController?) {
self.navigationController?.navigationBar.barTintColor = UIColor.red
super.willMove(toParentViewController: parent)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1432 次 |
最近记录: |