iOS-UITableView悬停处理

2017-06-02  本文已影响0人  coffeedou

主要是对悬浮条的处理,刚开始考虑在view上添加一份同样布局的view,通过隐藏和显示来实现,由于存在两个同样布局的view,处理按钮事件逻辑相对复杂,而用改变悬浮条Superview的方式,实现就比较简单。

iOS-UITableView悬停拉伸处理.gif

-(void)createTableView {

self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.showsVerticalScrollIndicator = NO;
 //没有把头视图添加到tableHeaderView上,而是设置contentInset来实现
self.tableView.contentInset = UIEdgeInsetsMake(OFFSETHEIGHT, 0, 0, 0);
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

[self.tableView addSubview:[self createHeaderView]];

}

-(UIView *)createHeaderView {

self.headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -OFFSETHEIGHT, SCREENW, OFFSETHEIGHT)];

self.headerImageView.image = [UIImage imageNamed:@"my_card_img_bg"];
// 这行对于实现拉伸放大效果很重要
self.headerImageView.contentMode = UIViewContentModeScaleAspectFill;
self.headerImageView.clipsToBounds = YES;
self.headerImageView.userInteractionEnabled = YES;
[self.headerImageView addSubview:self.btnView];

return  self.headerImageView;

}

-(UIView *)btnView {

if (!_btnView) {

    _btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, SCREENW, 30)];
    _btnView.backgroundColor = [UIColor grayColor];
   
    [self createButton:_btnView array:@[@"关注",@"群组",@"粉丝"] startTag:1000];
}
return  _btnView;

}

//添加标题按钮
-(void)createButton:(UIView *)view array:(NSArray *)arr startTag:(NSInteger)tag{

 CGFloat btnW = SCREENW / arr.count;
for (int i = 0; i < arr.count; i ++) {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * btnW, 0, btnW, 30)];
    [button setTitle:arr[i] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag + i;
    [view addSubview:button];
}

view.userInteractionEnabled = YES;

}

//监听tableview的滑动,处理拉伸放大和悬浮条,对于导航条透明度没做处理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGFloat offSetY = scrollView.contentOffset.y;

if (offSetY < -OFFSETHEIGHT) {
    self.navigationController.navigationBar.hidden = YES;
 //处理拉伸放大效果
    self.headerImageView.y = offSetY;
    self.headerImageView.height = -offSetY;
 //处理悬浮条
    self.btnView.y = OFFSETHEIGHT-30 + (-offSetY - OFFSETHEIGHT);

}else if (offSetY>= -94) {
//处理悬浮条
    self.btnView.y = 64;
    [self.view addSubview:self.btnView];
    self.navigationController.navigationBar.hidden = NO;
}else {
 //处理悬浮条
    self.btnView.y = OFFSETHEIGHT-30;
    [self.headerImageView addSubview:self.btnView];
    self.navigationController.navigationBar.hidden = YES;
}

}

上一篇下一篇

猜你喜欢

热点阅读