- _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), navH)];
- _topView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
- [self.view addSubview:_topView];
3、监听控制器的滚动,当滚动的 contentsize.y / x = alpha,x 为 Uiview 滑动到某个位置时,Uiview 能够被显示
- _topView.alpha = -offsetY / startImageViewH;
- _tableView = [[UITableView alloc] initWithFrame: CGRectMake(0, navH, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - navH) style: UITableViewStylePlain];
2、设置滑动的控制器 tableView 的 contentInset
- _tableView.contentInset = UIEdgeInsetsMake(startImageViewH, 0, 0, 0);
3、设置 imageView 的 frame 并且添加到_tableView 里
- UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -startImageViewH, CGRectGetWidth(self.view.frame), startImageViewH)]
- [self.tableView addSubview:imageView];
- - (void) scrollViewDidScroll: (UIScrollView * ) scrollView {
- CGFloat offsetY = scrollView.contentOffset.y;
- /* 计算往下拉的时候偏移量 **/
- if (offsetY < -startImageViewH) {
- /* 计算差值 **/
- CGFloat interval = -CGRectGetHeight(self.imageView.frame) - offsetY;
- NSLog(@"interval = %f", interval);
- /* imageView 的高度 增加 interval **/
- CGFloat imageViewH = CGRectGetHeight(self.imageView.frame) + interval;
- /* 计算 imageView的 高度增加后的,宽度增加后的值 **/
- CGFloat imageViewW = imageViewH * CGRectGetWidth(self.imageView.frame) / CGRectGetHeight(self.imageView.frame);
- /* 计算 imageView X 的偏移量 **/
- CGFloat imageViewX = self.imageView.frame.origin.x - (imageViewW - CGRectGetWidth(self.imageView.frame)) / 2;
- /* 计算 imageView Y 的偏移量 **/
- CGFloat imageViewY = self.imageView.frame.origin.y - interval;
- /* 赋值 **/
- self.imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
- } else {
- _topView.alpha = -offsetY / startImageViewH;
- }
- //其实思路很简单 就是 让tableView偏移 一图片的高度,然后在把图片添加到tableView中,然后再监听didScrollView,在里面改变图片的frame
- }
来源: http://www.jianshu.com/p/df27233c2bed