美文网首页程序员
iOS下拉放大图片上推导航栏渐变颜色

iOS下拉放大图片上推导航栏渐变颜色

作者: 哭与行 | 来源:发表于2017-05-17 11:40 被阅读0次

最近想到实现tableview下拉实现headerView图片根据偏移值放大,上推实现导航栏根据偏移值计算导航栏透明度.于是就试着写了下,写的不好还望多多包涵, 本人也是第二次写文章...

代码地址:https://github.com/kuyuxing/Gradient.git

效果:

viewDidLoad方法:

- (void)viewDidLoad {

[super viewDidLoad];

// 创建tableView

UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];

[self.view addSubview:tableView];

tableView.backgroundColor = [UIColor greenColor];

tableView.dataSource = self;

tableView.delegate = self;

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"hahaha" style:UIBarButtonItemStylePlain target:self action:nil];

self.navigationItem.leftBarButtonItem = item;

// 设置视图不自动下移

self.automaticallyAdjustsScrollViewInsets = NO;

// 设置导航栏为颜色

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

// 获取负责导航栏颜色显示的UIImageView,用于修改导航栏颜色

UIImageView *barImageView = self.navigationController.navigationBar.subviews.firstObject;

// 属性进行引用

self.barImageView = barImageView;

self.barImageView.alpha = 0;

}

scrollView滚动的方法:

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

// 偏移量上移0~200之间根据偏移量计算透明度

if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < 200) {

self.barImageView.alpha = scrollView.contentOffset.y / 200;

// 超过200设置为不透明,防止快速滑动导致透明度不正确问题

}else if(scrollView.contentOffset.y > 200){

self.barImageView.alpha = 1;

}else{

CGFloat contentOffsetY = ABS(scrollView.contentOffset.y);

// 下拉根据偏移设置图片放大,并设置透明

self.imageView.frame = CGRectMake(-contentOffsetY, -contentOffsetY, [UIScreen mainScreen].bounds.size.width + 2 * contentOffsetY, contentOffsetY + 250);

self.barImageView.alpha = 0;

}

}

其他部分就直接贴代码了

// tableView返回headerView的方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 250)];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:view.bounds];

self.imageView = imageView;

[view addSubview:imageView];

imageView.image = [UIImage imageNamed:@"02"];

return view;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 200;

}

// tableView数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 200;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *ID = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

}

cell.textLabel.text = @"国服第一JS";

return cell;

}

好像又写的有点啰嗦了,呵呵~😆

相关文章

网友评论

    本文标题:iOS下拉放大图片上推导航栏渐变颜色

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