QQ20171030-112122-HD.gif
最近看到hongxin的仿大麥項目滩届,受益匪淺,在此摘出導(dǎo)航欄隨著頂部廣告滾動漸變色的navbar細(xì)解一下凫佛。
1.首先將需要用到圖片加載處理SDWebImage和廣告循環(huán)類SDCycleScrollView兩個文件導(dǎo)入項目晾浴。
2.為了簡單化,將類擴(kuò)展文件Extension加入項目胚宦,這個文件中含有btn,view燕垃,color的擴(kuò)展方法
3.自定義tableview的headerView枢劝,寫入廣告滾動視圖。并且創(chuàng)建他的代理方法(當(dāng)廣告滾動到下一張時實現(xiàn))
- (void)cycleScrollViewDidScrollViewToIndexforUrl:(NSString *)url;
4.controller里面
4_1.創(chuàng)建tableview卜壕。
4_2.創(chuàng)背景變色視圖您旁,注意:這個變色視圖一定要在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.實現(xiàn)headerView代理方法(實現(xiàn)導(dǎo)航欄隨廣告滾動變色效果)
- (void)cycleScrollViewDidScrollViewToIndexforUrl:(NSString *)url
{
[self.bgImg sd_cancelCurrentAnimationImagesLoad];
[self.bgImg sd_setImageWithURL:[NSURL URLWithString:url]];
//self.bgImg.image = [UIImage imageNamed:url];
}
5.實現(xiàn)scrollView代理方法(實現(xiàn)導(dǎo)航欄上隱下現(xiàn)效果印叁,與主題無關(guān)被冒,不需要可以直接注釋)
-(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;
}