【问题标题】:Animate Color Navigation Bar when ScrollView Down向下滚动视图时动画颜色导航栏
【发布时间】:2019-11-29 16:15:00
【问题描述】:

在我的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];
}

带有collectionView的视图控制器收到委托

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

【问题讨论】:

    标签: ios objective-c uitableview uicollectionview uiscrollview


    【解决方案1】:

    尝试在动画块之外设置您想要动画的属性(确保首先调用 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,它应该更新其背景属性,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多