navigationBar根据滑动距离实现渐变色

本人最开始实现的方法是在视图即将显现的方法里面添加隐藏navigationBar,然后

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

self.navigationController.navigationBar.hidden=NO;

 CGFloat offsetY = self.scrollView.contentOffset.y;

self.navigationController.navigationBar.alpha = offsetY/100;

}

这样写纯粹是取巧了  效果是//  先是纯透明  然后逐渐显示。

后面学习的方法:

navigationBar变为纯透明

//第一种方法

//导航栏纯透明

[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

//去掉导航栏底部的黑线

self.navigationBar.shadowImage = [UIImage new];

//第二种方法

[[self.navigationBar subviews] objectAtIndex:0].alpha = 0;

tabBar同理

[self.tabBar setBackgroundImage:[UIImage new]];

self.tabBar.shadowImage = [UIImage new];


navigationBar根据滑动距离的渐变色实现

//第一种

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGFloat offsetToShow = 200.0;//滑动多少就完全显示

CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;

[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha;

}

//第二种

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGFloat offsetToShow = 200.0;

CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;

[self.navigationController.navigationBar setShadowImage:[UIImage new]];

[self.navigationController.navigationBar

setBackgroundImage:[self imageWithColor:[[UIColor

orangeColor]colorWithAlphaComponent:alpha]]

forBarMetrics:UIBarMetricsDefault];

}

//生成一张纯色的图片

- (UIImage *)imageWithColor:(UIColor *)color

{

CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return theImage;

}

亲测有效哦。有更好的方法,欢迎留言。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容