微博个人主页View交互方式

昨天有个需要要求像微博个人主页的view交互方式那样,(描述起来有点麻烦,具体请看下面的动态效果)!在网上没有找到实现方法,于是自己就探究了该交互的实现方式!


2016-11-09 22_19_42.gif

方法一:

先在self.view上添加一个tableView和一个topView,然后设置self.tableView.contentInset.top为topView的高度,然后在tableView的滚动方法里移动topView到相应的位置,在合适的位置不在让topView移动即可!(先设置contentInset后添加代理的原因是contentInset会触发scrollViewDidScroll!)

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(160, 0, 0, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat newConstant = -scrollView.contentOffset.y - 160;
    if (newConstant < -130) {
        newConstant = -130;
    }
// 如果判断下面的内容,tableView向下滚动时,topView不会跟着移动
//    if (newConstant > 0) {
//        newConstant = 0;
//    }
    self.topLayout.constant = newConstant;
}

方法二:

利用tableView的headerView的粘性.如果是在tableView的代理方法里设置headerView,当滚动到headerView的顶端后, headerView不会再跟随cell一起滚动!我们可以利用这个特性实现现在的需求!这个方式与我另一篇关于去除headerView粘性的文章内容差不多!具体上代码

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight =  130;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160)];
    view.backgroundColor = [UIColor redColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, self.view.frame.size.width, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"haha";
    [view addSubview:label];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 160;
}

Demo地址:https://github.com/fengyunjue/scrollViewDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,974评论 3 38
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 10,249评论 1 9
  • 熙熙攘攘的失物招领站,大家踮着脚尖,努力寻找着曾经属于自己的东西。 一、 阿荣已经记不清有多少个夜晚是在失眠中度过...
    PhoenixWright阅读 3,523评论 0 1
  • typedef是类型定义的意思。typedef struct 是为了使用这个结构体方便。 具体区别在于: 若str...
    smile刺客阅读 12,706评论 1 2
  • Trust the child’s ability to learn and develop at their o...
    Betty_黄阅读 4,444评论 0 0

友情链接更多精彩内容