向下滚动视图时动画颜色导航栏
Animate Color Navigation Bar when ScrollView Down
在我的 View Controller
中,我有一个包含 3 个单元格和水平滚动的 collectionView ..
在每个单元格中我有一个 TableView
我在 view controller
中更改导航栏颜色的动画有问题。
为了管理方法 - (void) scrollViewDidScroll: (UIScrollView *) scrollView
我创建了一个委托来管理导航栏的颜色变化..
我的问题是我无法将导航栏的 alpha 颜色用作参考 scrollView.contentOffset.y
...颜色会立即更改,但不会根据 scrollView 的 contentOffset 设置动画。
谁能帮我找出我错在哪里?
CollectionView 单元格内的 TableView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.delegate cellScrollDownWithOffset:scrollView.contentOffset.y];
}
View Controller with collectionView received delegate
#pragma mark - BaseVerticalCell delegate
-(void)cellScrollDownWithOffset:(CGFloat)offset {
[UIView animateWithDuration:.3 animations:^{
UIColor *opacheColor = [UIColor colorWithHexString:@"#F9F9FA" setAlpha:offset /150];
UIColor *defaultColor = [UIColor colorWithWhite:1 alpha:offset /150];
self.navigationController.navigationBar.barTintColor = offset/ 150 > 0 ? opacheColor : defaultColor;
}];
}
尝试在动画块之外设置要设置动画的属性(确保首先调用 layoutIfNeeded 以确保所有尚未提交的未决布局调整已完全布置),然后设置动画的布局导航栏(而不是动画 属性 变化)。这是一个例子:
- (IBAction)animateNavBarColor:(id)sender {
[self.navigationController.navigationBar layoutIfNeeded];
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
[UIView animateWithDuration:2.0f animations:^{
[self.navigationController.navigationBar layoutIfNeeded];
}];
}
所以特别针对您的场景,请尝试将您的代码调整为:
- (void)cellScrollDownWithOffset:(CGFloat)offset {
[self.navigationController.navigationBar layoutIfNeeded];
UIColor *opacheColor = [UIColor colorWithHexString:@"#F9F9FA" setAlpha:offset /150];
UIColor *defaultColor = [UIColor colorWithWhite:1 alpha:offset /150];
self.navigationController.navigationBar.barTintColor = offset/ 150 > 0 ? opacheColor : defaultColor;
[UIView animateWithDuration:.3 animations:^{
[self.navigationController.navigationBar layoutIfNeeded];
}];
}
在 WWDC 2012 session 中有一些关于使用此方法制作动画的参考资料:https://developer.apple.com/videos/play/wwdc2012/228/ 关于 "Best Practices for Mastering Autolayout"
这是 Apple 开发者论坛上的另一个参考资料:https://forums.developer.apple.com/thread/60258 特别与导航栏颜色的动画相关——特别是 Rincewind 的这部分回复:
If you call -layoutIfNeeded on the navigation bar during the animation block it should update its background properties,
在我的 View Controller
中,我有一个包含 3 个单元格和水平滚动的 collectionView ..
在每个单元格中我有一个 TableView
我在 view controller
中更改导航栏颜色的动画有问题。
为了管理方法 - (void) scrollViewDidScroll: (UIScrollView *) scrollView
我创建了一个委托来管理导航栏的颜色变化..
我的问题是我无法将导航栏的 alpha 颜色用作参考 scrollView.contentOffset.y
...颜色会立即更改,但不会根据 scrollView 的 contentOffset 设置动画。
谁能帮我找出我错在哪里?
CollectionView 单元格内的 TableView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.delegate cellScrollDownWithOffset:scrollView.contentOffset.y];
}
View Controller with collectionView received delegate
#pragma mark - BaseVerticalCell delegate
-(void)cellScrollDownWithOffset:(CGFloat)offset {
[UIView animateWithDuration:.3 animations:^{
UIColor *opacheColor = [UIColor colorWithHexString:@"#F9F9FA" setAlpha:offset /150];
UIColor *defaultColor = [UIColor colorWithWhite:1 alpha:offset /150];
self.navigationController.navigationBar.barTintColor = offset/ 150 > 0 ? opacheColor : defaultColor;
}];
}
尝试在动画块之外设置要设置动画的属性(确保首先调用 layoutIfNeeded 以确保所有尚未提交的未决布局调整已完全布置),然后设置动画的布局导航栏(而不是动画 属性 变化)。这是一个例子:
- (IBAction)animateNavBarColor:(id)sender {
[self.navigationController.navigationBar layoutIfNeeded];
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
[UIView animateWithDuration:2.0f animations:^{
[self.navigationController.navigationBar layoutIfNeeded];
}];
}
所以特别针对您的场景,请尝试将您的代码调整为:
- (void)cellScrollDownWithOffset:(CGFloat)offset {
[self.navigationController.navigationBar layoutIfNeeded];
UIColor *opacheColor = [UIColor colorWithHexString:@"#F9F9FA" setAlpha:offset /150];
UIColor *defaultColor = [UIColor colorWithWhite:1 alpha:offset /150];
self.navigationController.navigationBar.barTintColor = offset/ 150 > 0 ? opacheColor : defaultColor;
[UIView animateWithDuration:.3 animations:^{
[self.navigationController.navigationBar layoutIfNeeded];
}];
}
在 WWDC 2012 session 中有一些关于使用此方法制作动画的参考资料:https://developer.apple.com/videos/play/wwdc2012/228/ 关于 "Best Practices for Mastering Autolayout"
这是 Apple 开发者论坛上的另一个参考资料:https://forums.developer.apple.com/thread/60258 特别与导航栏颜色的动画相关——特别是 Rincewind 的这部分回复:
If you call -layoutIfNeeded on the navigation bar during the animation block it should update its background properties,