美文网首页
导航栏渐变,下拉放大图片

导航栏渐变,下拉放大图片

作者: haoxuan_xia | 来源:发表于2017-12-06 16:09 被阅读0次

直接把图片添加在tableview上面,并不是设置tableview.headerView为图片


定义几个通用宏

// iPhone X

#define  KP_iPhoneX ([[UIScreen mainScreen] bounds].size.width == 375.f && [[UIScreen mainScreen] bounds].size.height  == 812.f ? YES : NO)

#define HeadHeight 264 //头部视图的高度

#define navHeight (KP_iPhoneX ? 88 : 64) //导航栏高度

#define oriOfftY  HeadHeight - navHeight //偏移量

头部视图的高度设置为你的xib高度。imageView的模式设置为Aspect Fill,不然缩放放大图片的时候回压缩


设置tableview的内容偏移量// 当调用contentInset会自动调用scrollViewDidScroll

self.tableView.contentInset = UIEdgeInsetsMake(oriOfftY, 0, 0, 0);


在scrollViewDidScroll中修改透明度及设置图片的高度

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

//求偏移量,一开始偏移量是负值,转为它的相反数

CGFloat offset = - scrollView.contentOffset.y;

if (offset < navHeight) {

offset = navHeight;

}

self.heightConstr.constant = offset;

//透明度/

CGFloat alpha = 1 - (offset - navHeight)/(oriOfftY);

if (alpha >= 1) {

alpha = 1;

}

//拿到标题

UILabel *titleL = (UILabel *)self.navigationItem.titleView;

titleL.textColor = [UIColor colorWithWhite:0 alpha:alpha];

//把颜色生成图片

UIColor *alphaColor = [UIColor colorWithWhite:1 alpha:alpha];

UIColor *showColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:alpha] ;

//把颜色生成图片

UIImage *alphaImage = [UIImage imageWithColor:alphaColor];

UIImage *showImage = [UIImage imageWithColor:showColor];

//修改导航条背景图片

[self.navigationController.navigationBar setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault];

//设置导航栏阴影线

[self.navigationController.navigationBar setShadowImage:showImage];

}

//注:代码直接那去就可以用,主要操作是在- (void)scrollViewDidScroll:(UIScrollView *)scrollView中

相关文章

网友评论

      本文标题:导航栏渐变,下拉放大图片

      本文链接:https://www.haomeiwen.com/subject/rlclixtx.html