duc*_*i9y 9 objective-c uinavigationcontroller ios ios6
当按下某个控制器时,我正在更改导航栏的背景图像.我想为这种变化制作动画.我可以使用以下代码执行此操作:
[UIView transitionWithView:self.navigationController.navigationBar
duration:0.3f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"now-playing-nav-bar.png"]
forBarMetrics:UIBarMetricsDefault];
} completion:NULL];
Run Code Online (Sandbox Code Playgroud)
但是,这会阻止应用于navigationBarTitle和UIBarButtonItems 的默认推送动画.
如何让背景更改和推动动画一起工作?
我希望尽可能使用vanilla作为解决方案.
PS:我无法使用,tintColor因为背景是纹理的.
duc*_*i9y 26
仅在iOS 6上测试
我使用Core Animation解决了它:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CATransition *animation = [CATransition animation];
animation.duration = 0.3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionFade;
[self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
[UIView animateWithDuration:0.3 animations:^{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"now-playing-nav-bar.png"] forBarMetrics:UIBarMetricsDefault];
}];
}
Run Code Online (Sandbox Code Playgroud)
做同样的事viewWillDisappear:,你很高兴.
这适用于iOS 9:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
changeNavigationBackground()
}
func changeNavigationBackground() {
// navigationBar should always exist if the view controller is in a UINavigationController
guard let navigationBar = navigationController?.navigationBar else { return }
// the current navigation background
guard let imageView = navigationBar.subviews.filter({ $0 is UIImageView }).first else { return }
let image = UIImage(named: "navigation_background")
let newImageView = UIImageView(image: image)
newImageView.frame = imageView.frame
newImageView.alpha = 0.0
navigationBar.insertSubview(newImageView, belowSubview: imageView)
transitionCoordinator()?.animateAlongsideTransition({ (context) in
imageView.alpha = 0.0
newImageView.alpha = 1.0
}, completion: { (context) in
newImageView.removeFromSuperview()
navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
imageView.alpha = 1.0
})
}
Run Code Online (Sandbox Code Playgroud)
我尝试尽可能避免使用Core Animation,所以我只使用了视图并在转换过程中更改了它们的alpha值.
| 归档时间: |
|
| 查看次数: |
3983 次 |
| 最近记录: |