博客> 知乎状态栏滚动效果
知乎状态栏滚动效果
2019-07-18 00:06 评论:0 阅读:772 ios_wj@163.com
ios scrollView 滚动
  • `- 1. - _ 项目中要实现知乎三个状态栏类似的效果,网上搜了一片并没有找到合适的,只能自己写了。

    主要思想是利用tableView的contentInset和contentOffset,判断向上滑动时,headerview收起,tableview顶部至headerview,向下滑动时headerview展开,tableview顶部至headerView。

注意:tableView一定要在headerView的底层,不然就会盖住headerView,显示不出来。

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.title = @"我是title";

    [self.view insertSubview:self.headerView atIndex:1]; [self.view insertSubview:self.mainTableView atIndex:0]; }

-(UIView *)headerView { if (!_headerView) { _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, kNavHeight,kWidth_Screen, kHeaderViewHeight)]; _headerView.backgroundColor = [UIColor orangeColor]; } return _headerView; }

-(UITableView *)mainTableView { if (!_mainTableView) { _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavHeight, kWidth_Screen, kHeight_Screen-kHeaderViewHeight) style:UITableViewStylePlain]; _mainTableView.delegate = self; _mainTableView.dataSource = self; //tableview默认向下滚动kkHeaderViewHeight _mainTableView.contentInset = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0); _mainTableView.contentOffset = CGPointMake(0, -kHeaderViewHeight);

}
return _mainTableView;

}

pragma mark - scroll view delegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { lastScrollOffsetY = scrollView.contentOffset.y; }

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetY = scrollView.contentOffset.y; if (offsetY < 0> scrollView.contentSize.height) { return; } if (offsetY > lastScrollOffsetY && !headerViewHidden)//往上滑动 { //向上滑动,headerview收起,tableview顶部至headerview [UIView animateWithDuration:.25 animations:^{ [self.headerView setY:kNavHeight-kHeaderViewHeight]; self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);; }]; headerViewHidden = YES;

}
else if(offsetY &lt; lastScrollOffsetY && headerViewHidden)//往下滑动
{
    //向下滑动,headerview展开,tableview顶部至headerView
    [UIView animateWithDuration:.25 animations:^{
        [self.headerView setY:kNavHeight];
        self.mainTableView.contentInset = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0);;
    }];
    headerViewHidden = NO;
}

}> > quote here- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.title = @"我是title";

[self.view insertSubview:self.headerView atIndex:1];
[self.view insertSubview:self.mainTableView atIndex:0];

}

-(UIView *)headerView { if (!_headerView) { _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, kNavHeight,kWidth_Screen, kHeaderViewHeight)]; _headerView.backgroundColor = [UIColor orangeColor]; } return _headerView; }

-(UITableView *)mainTableView { if (!_mainTableView) { _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavHeight, kWidth_Screen, kHeight_Screen-kHeaderViewHeight) style:UITableViewStylePlain]; _mainTableView.delegate = self; _mainTableView.dataSource = self; //tableview默认向下滚动kkHeaderViewHeight _mainTableView.contentInset = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0); _mainTableView.contentOffset = CGPointMake(0, -kHeaderViewHeight);

}
return _mainTableView;

}

pragma mark - scroll view delegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { lastScrollOffsetY = scrollView.contentOffset.y; }

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetY = scrollView.contentOffset.y; if (offsetY < 0> scrollView.contentSize.height) { return; } if (offsetY > lastScrollOffsetY && !headerViewHidden)//往上滑动 { //向上滑动,headerview收起,tableview顶部至headerview [UIView animateWithDuration:.25 animations:^{ [self.headerView setY:kNavHeight-kHeaderViewHeight]; self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);; }]; headerViewHidden = YES;

}
else if(offsetY &lt; lastScrollOffsetY && headerViewHidden)//往下滑动
{
    //向下滑动,headerview展开,tableview顶部至headerView
    [UIView animateWithDuration:.25 animations:^{
        [self.headerView setY:kNavHeight];
        self.mainTableView.contentInset = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0);;
    }];
    headerViewHidden = NO;
}

}

收藏
0
sina weixin mail 回到顶部