关闭

tableView位置的动态改变

标签: ios
108人阅读 评论(0) 收藏 举报
分类:

#import "DetailViewController.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
    BOOL _isShowImageView; //背景图片是否显示(即tableView是否滑到顶部)
    BOOL _isMoveWithContentSize; //是否根据contentSize的改变来移动tableView
    BOOL _isDownInertia;   //是否有向下的惯性
    CGRect _startFrame; //初始tableView的y值是300
    CGRect _endFrame;   //结束tbaleView的y值是0
    /**
     *  初始contentSize的y值
     */
    CGFloat _orientation;
}
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isShowImageView = YES;
    // Do any additional setup after loading the view from its nib.
}

-(void)viewDidAppear:(BOOL)animated{
    _startFrame = self.tableview.frame;
    _endFrame = _startFrame;
    _endFrame.origin.y = 60;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *myCell = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:myCell];
    }
    cell.textLabel.text = @"测试";
    cell.detailTextLabel.text = @"测试数据";
    return cell;
}

#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"标题";
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

#pragma mark UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.frame.origin.y == 0) {
        _isShowImageView = NO;
    }else if (scrollView.frame.origin.y == 300){
        _isShowImageView = YES;
    }
    CGRect tempRect = scrollView.frame;
    if (_isShowImageView) {
        //向上滑还是向下滑
        if (scrollView.contentOffset.y > _orientation) {
            _isMoveWithContentSize = YES;
        }else{
            //只要tableView的位置在0-300之间就跟着contentSize滑动
            if (scrollView.frame.origin.y < 0 || scrollView.frame.origin.y > 300) {
                _isMoveWithContentSize = NO;
            }
        }
    }else{
        if (scrollView.contentOffset.y > _orientation || scrollView.contentOffset.y > 0) {
            if (scrollView.frame.origin.y < 0 || scrollView.frame.origin.y > 300) {
                _isMoveWithContentSize = NO;
            }
        }else{
                _isMoveWithContentSize = YES;
        }
    }
    if (_isDownInertia) {
        _isMoveWithContentSize = NO;
    }
    //根据contentSize的改变来改变scollView的frame,同时将contentSize置为0(感觉滑动整个scrollView)
    if (_isMoveWithContentSize) {
        tempRect.origin.y = scrollView.frame.origin.y - scrollView.contentOffset.y;
        scrollView.frame = tempRect;
        scrollView.contentOffset = CGPointMake(0, 0);
    }
}

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

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    //velocity:(惯性)速度,有正负,可以判断向上向下滑动
    NSLog(@"~~~~~~%@",NSStringFromCGPoint(velocity));
    NSLog(@"---%@ +++%@",NSStringFromCGPoint(scrollView.contentOffset),NSStringFromCGPoint(*targetContentOffset));
    if (!_isShowImageView && velocity.y < 0) {
        _isDownInertia = YES;
    }
    if (_isShowImageView && velocity.y > 0) {
        //防止惯性滑动
        *targetContentOffset = scrollView.contentOffset;
    }
    __block CGRect rect = scrollView.frame;
    [UIView transitionWithView:scrollView duration:0.2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        if (_isShowImageView) {
            if (scrollView.frame.origin.y < _startFrame.origin.y * 0.9) {
                //tableView移动到顶部
                rect.origin.y = 0.0;
                scrollView.frame = rect;
            }else{
                //tableView回到原位置(即显示背景图片的位置)
                rect.origin.y = _startFrame.origin.y;
                scrollView.frame = rect;
            }
        }else{
            if (scrollView.frame.origin.y < _startFrame.origin.y * 0.1) {
                //tableView移动到顶部
                rect.origin.y = 0.0;
                scrollView.frame = rect;
            }else{
                //tableView回到原位置(即显示背景图片的位置)
                rect.origin.y = _startFrame.origin.y;
                scrollView.frame = rect;
            }
        }
    } completion:nil];
}



@end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3202次
    • 积分:138
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档