手势拖动tableview

可以实现向下拖动tableview的时候,tableview不滚动,但frame改变,松手会回弹,在tableview向上滚动一段距离的情况下,拖动tableview,offset改变,frame不变

1.首先要让添加的手势和原本的手势都起作用

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if ([otherGestureRecognizer.view isKindOfClass:[UITableView class]]) {

        return YES;

    }

        return NO;

    }

2.在添加的手势在began的时候禁掉tableview的滚动

- (void)heihei:(UIPanGestureRecognizer *)pan

{

    if (pan.enabled == NO) {

        return ;

    }

    CGPoint translation = [pan translationInView:pan.view];

    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, 0, translation.y);

    [pan setTranslation:CGPointZero inView:pan.view];

    //滑到顶部时,上移

    if (_tableView.frame.origin.y < 0) {

            _tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen             mainScreen].bounds.size.height);

        //超出边界 禁掉手势

        pan.enabled = NO;

}

if (pan.state == UIGestureRecognizerStateBegan) {

        _tableView.scrollEnabled = NO;

} else if (pan.state == UIGestureRecognizerStateEnded) {

    //手势结束 回复手势

    pan.enabled = YES;

    _tableView.scrollEnabled = YES;

    [UIView animateWithDuration:0.5 animations:^{

            _tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen     mainScreen].bounds.size.height);

    }];

    }

}

3.在滚动结束后根据offset判断是否要禁掉添加的手势

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    if (_tableView.contentOffset.y > 0) {

        _pan.enabled = NO;    

        _tableView.scrollEnabled = YES;

    } else {

        _pan.enabled = YES;

    }

}

你可能感兴趣的:(手势拖动tableview)