将UINavigationBar从纯色动画为透明色

时间:2015-11-05 14:43:36

标签: ios objective-c uinavigationbar transition-coordinator

我试图在转换过程中将此应用的导航栏从纯色动画为透明色。

新视图控制器的barStyletranslucent属性也会发生变化。

我这样做的原因是因为我们使用透明的导航栏并在滚动期间将其淡入...

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat maxY = self.headerHeight - 64;

    CGFloat percentage = MIN(0.996, MAX(0, [self alphaFromPercentageWithEasing:offsetY / maxY]));

    [self.navigationController.navigationBar setBackgroundImage:[UIColor imageFromColor:self.themeColor alpha:percentage] forBarMetrics:UIBarMetricsDefault];
}

滚动时效果很好,但为了获得透明导航栏,您必须使用backgroundImage的{​​{1}}属性。如果您尝试使用navigationBar小于1的颜色,则使用颜色为1的Alpha。因此您不能使用透明颜色。

我遇到的问题是在转换过程中我有alpha这样做......

transitionCoordinator

显然,这会将颜色从原始颜色设置为具有100%alpha的视图控制器[self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { self.navigationController.navigationBar.barTintColor = self.themeColor; } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { // this is so that when popping back to here the bar alpha // is set correctly from the current scroll position [self scrollViewDidScroll:self.tableView]; }]; 属性,然后在转换完成时themeColor消失。

我甚至尝试过在图像之间制作动画......

navigationBar

但这只是将它设置为0%alpha而没有动画的最终图像。

我正在寻找一种方法,可以在过渡过程中从原始颜色的100%alpha动画到[self.navigationController.navigationBar setBackgroundImage:[UIColor imageFromColor:originalColor alpha:1.0] forBarMetrics:UIBarMetricsDefault]; [self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { [self.navigationController.navigationBar setBackgroundImage:[UIColor imageFromColor:self.themeColor alpha:0] forBarMetrics:UIBarMetricsDefault]; } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { // this is so that when popping back to here the bar alpha // is set correctly from the current scroll position [self scrollViewDidScroll:self.tableView]; }]; 的0%alpha。因此,条形图将从(例如)蓝色到红色以及1.0 alpha到0.0 alpha进行动画处理。

这甚至可能吗?谁知道怎么做?

0 个答案:

没有答案