跟着广告滚动变色navbar

QQ20171030-112122-HD.gif

最近看到hongxin的仿大麦项目,受益匪浅,在此摘出导航栏随着顶部广告滚动渐变色的navbar细解一下。

1.首先将需要用到图片加载处理SDWebImage和广告循环类SDCycleScrollView两个文件导入项目。

2.为了简单化,将类扩展文件Extension加入项目,这个文件中含有btn,view,color的扩展方法

3.自定义tableview的headerView,写入广告滚动视图。并且创建他的代理方法(当广告滚动到下一张时实现)

- (void)cycleScrollViewDidScrollViewToIndexforUrl:(NSString *)url;

4.controller里面

4_1.创建tableview。

4_2.创背景变色视图,注意:这个变色视图一定要在tableView下面,所以这里用了[self.view insertSubview:bgImg atIndex:0];

[self.view addSubview:self.tableView];

UIImageView *bgImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, -5, ScreenWidth + 10, ADHeight + 10)];
bgImg.centerX = ScreenWidth / 2;
bgImg.image = [UIImage imageNamed:self.imgArr[0]];
bgImg.contentMode = UIViewContentModeScaleAspectFill;
[bgImg sd_setImageWithURL:self.imgArr[0]];
[self.view insertSubview:bgImg atIndex:0];

UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = bgImg.bounds;
[bgImg addSubview:effectView];
self.effectView = effectView;

UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, NAVBAR_HEIGHT)];
maskView.backgroundColor = [[UIColor colorWithHexString:@"#e2f9444"] colorWithAlphaComponent:0];
[self.view insertSubview:maskView atIndex:1];
self.bgImg = bgImg;

4_3.实现headerView代理方法(实现导航栏随广告滚动变色效果)

- (void)cycleScrollViewDidScrollViewToIndexforUrl:(NSString *)url
{
[self.bgImg sd_cancelCurrentAnimationImagesLoad];
[self.bgImg sd_setImageWithURL:[NSURL URLWithString:url]];
//self.bgImg.image = [UIImage imageNamed:url];
}

5.实现scrollView代理方法(实现导航栏上隐下现效果,与主题无关,不需要可以直接注释)

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat alpha = offsetY / 100;

[self.navBar setBackgroundImage:[UIImage imageWithColor:[BASECOLOR colorWithAlphaComponent:alpha] size:CGSizeMake(ScreenWidth, NAVBAR_HEIGHT)] forBarMetrics:UIBarMetricsDefault];

CGFloat H = ScreenWidth / 16 * 9 + 10 - offsetY;

if (offsetY > 0) {
H = ScreenWidth / 16 * 9 + 10;
}
self.bgImg.frame = CGRectMake(-5, -5, ScreenWidth + 10, H);
self.effectView.frame = self.bgImg.bounds;
}

详见demo:https://github.com/1192484280/Gradient-color-navBar

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • runtime 简介 传说中的 runtime, 从开始接触时就听说.但是很少真正用到.了解了 runtime, ...
    明月钓无痕阅读 424评论 0 1
  • Lancyandkaxifa阅读 99评论 0 0
  • 大叔开启了《每周一位多肉达人的故事》系列,分享养肉人的心得、经验、经历。虽然说是达人,但是不管你是新手还是老鸟都可...
    多肉花客阅读 932评论 0 1
  • 一年一度端午节,粽叶又飘香。 每到此刻,各大商塲都会应景推出专买区。品种齐全,形色多样,各地风味都有。很有诱惑力。...
    浪淘沙0706阅读 253评论 0 0