Ios 向下滚动视图时设置颜色导航栏的动画

Ios 向下滚动视图时设置颜色导航栏的动画,ios,objective-c,uitableview,uicollectionview,uiscrollview,Ios,Objective C,Uitableview,Uicollectionview,Uiscrollview,在我的视图控制器中,我有一个具有3个单元格和水平滚动的collectionView 在每个单元格中,我都有一个TableView 我在更改视图控制器中导航栏的颜色时遇到动画问题 要管理方法-void scrollView didcroll:UIScrollView*scrollView,我创建了一个委托来管理导航栏的颜色更改 我的问题是,我无法将导航栏的alpha颜色用作参考scrollView.contentOffset.y来设置动画。。。颜色会立即更改,但不会基于scrollView的con

在我的视图控制器中,我有一个具有3个单元格和水平滚动的collectionView

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

我在更改视图控制器中导航栏的颜色时遇到动画问题

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

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

有人能帮我找出哪里错了吗

CollectionView单元格内的TableView

接收到collectionView委托的视图控制器


尝试在动画块外设置要设置动画的特性,确保首先调用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课程:关于掌握自动布局的最佳实践中,有一些关于使用此方法制作动画的参考资料

以下是苹果开发者论坛上的另一个参考:特别是与导航栏颜色动画相关的——特别是Rincewind的这部分回应:

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

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