向下滚动视图时

时间:2019-07-21 12:33:38

标签: ios objective-c uitableview uicollectionview uiscrollview

在我的View Controller中,我有一个3个单元格且水平滚动的collectionView。

在每个单元格中,我都有一个TableView

我对动画有问题,无法更改view controller中导航栏的颜色。

要管理方法- (void) scrollViewDidScroll: (UIScrollView *) scrollView,我创建了一个委托来管理导航栏的颜色更改。.

我的问题是我无法使用scrollView.contentOffset.y作为参考来为导航栏的Alpha颜色设置动画...颜色会立即更改,但不会基于scrollView的contentOffset进行动画处理。

有人可以帮我弄清楚我错了吗?

CollectionView单元格内的TableView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.delegate cellScrollDownWithOffset:scrollView.contentOffset.y];
}

具有collectionView的View Controller收到了委托

#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;
    }];
}

1 个答案:

答案 0 :(得分:0)

尝试在动画块外部设置要设置动画的属性(确保先调用layoutIfNeeded,以确保未布局的所有未提交的布局调整都被完全布局),然后为navBar的布局设置动画(而不是对属性更改进行动画处理)。这是一个示例:

- (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会话中有一些使用这种方法进行动画制作的参考资料:https://developer.apple.com/videos/play/wwdc2012/228/关于“掌握自动布局的最佳实践”

这里是Apple开发者论坛上的另一本参考书:https://forums.developer.apple.com/thread/60258与使navBar颜色动画化有关-特别是Rincewind响应的这一部分:

  

如果在动画块期间在导航栏上调用-layoutIfNeeded,则应该更新其背景属性,

相关问题