gpt4 book ai didi

iOS 10 barTintColor 动画

转载 作者:技术小花猫 更新时间:2023-10-29 10:45:18 24 4
gpt4 key购买 nike

我注意到 ios 10 中条形色调颜色的动画方式发生了变化。我创建了一个示例项目来概述变化:Github: ios10BarTintDemo

基本上在 ios 9 上,barTintColor 使用 [UIViewControllerTransitionCoordinator animateAlongsideTransition]

平滑地设置动画

但是在 ios 10 上,动画不太流畅,当弹出 View Controller 时根本没有动画,我尝试添加 [self.navigationController.navigationBar layoutIfNeeded] 一些类似的答案,但这在推送/弹出 Controller 时似乎没有任何效果。

最佳答案

更新

我已经在 iOS 10.3 中进行了测试,我认为问题已解决。并且 transitionCordinator 不再需要了。我觉得动画很流畅。请查看我的project on 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
}
}

============================================= ================================================ ================================================ ================================================ ================================================ ================================================ ===

要实现这种动画,您应该使用 UIViewControllerTransitionCoordinator 作为 Apple documentation说它是:

An object that adopts the UIViewControllerTransitionCoordinator protocol provides support for animations associated with a view controller transition.(...)

所以每个 UIViewController 都有自己的 transitionController。为此,您应该在 UIViewControllerClass 中调用:

self.transitionCoordinator()

来自 documentation :

Returns the active transition coordinator object.

因此,为了获得您想要的结果,您应该在 viewController transitionCoordinatior 中实现 animateAlongsideTransition 方法。当您单击 backButton 并向后滑动时,动画会起作用。

示例:

navigation_bar_animation

第一个 Controller :

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

第二个 Controller :

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
}

}

更新 iOS 10

在 iOS 10 中,棘手的部分是在 second ViewController 中添加 willMoveTo(parentViewController parent: UIViewController?)。并将导航栏 tintColor 设置为 previous Controller 的颜色值。此外,在 second ViewControler 的 viewDidAppear 方法中,将 navigationBar.tintColor 设置为 第二个 viewController。

查看我的示例 project on github

关于iOS 10 barTintColor 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39728460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com