我需要在某个时候更改视图中navBar
的背景图片
let animation = CATransition()
override func viewDidLoad() {
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation.type = kCATransitionFade
animation.duration = 0.5
}
在scrollViewDidScroll()
func我应该更改backgroundImage
override func scrollViewDidScroll() {
if someCondition {
showNavigationBar()
} else {
hideNavigationBar()
}
}
func showNavigationBar() {
self.navigationController?.navigationBar.layer.add(animation, forKey: nil)
UIView.animate(withDuration: 0.4, animations: {
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
}, completion: nil)
}
}
这里发生的问题是这个块中的动画没有发生。但是,在调试编译器时运行这个指令,但是没有动画。 :(
func hideNavigationBar() {
UIView.animate(withDuration: 0.4, animations: {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
}, completion: nil)
}
}
那么问题是什么,有什么想法吗?
答案 0 :(得分:0)
我想出了如何实现这一点,我刚刚向hideNavigationBar()添加了一行,显然在更改navBar
背景后,动画消失了。所以我需要重新添加它':)
func hideNavigationBar() {
self.navigationController?.navigationBar.layer.add(animation, forKey: nil)
UIView.animate(withDuration: 0.4, animations: {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
}, completion: nil)
}