uiscrollview(uitableview)和手势冲突解决

在有uiscrollview或者其子类的view上添加手势冲突的解决办法

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
        pan.delegate = self;
        [self addGestureRecognizer:pan];
/**
 *  实现拖动手势方法
 *
 *  @param panGestureRecognizer 手势本身
 */
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer{

    CGPoint translation = [panGestureRecognizer translationInView:self];
    NSLog(@"~~~拖拽手势:translation.x:%f,translation.y:%f~~~",translation.x,translation.y);
    
    if (translation.y > 0) {
        self.frame = CGRectMake(0, translation.y, 375, 667);
        if (translation.y >= 200) {
            [self removeFromSuperview];
        }
    }
    
    if (translation.x > 0) {
        self.frame = CGRectMake(translation.x, 0, 375, 667);
        if (translation.x >= 200) {
            [self removeFromSuperview];
        }
    }
    
    if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        if (translation.y < 200 || translation.x < 200) {
            self.frame = CGRectMake(0, 0, 375, 667);
        }
    }
    
}

#pragma  mark -- 手势代理,解决scrollview和手势的冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    return YES;//返回yes不阻断手势,返回NO阻断手势,根据具体情况处理
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
//此处的otherGesture是指scrollview或者其子类上边自带的手势,再根据具体的需求,我此处是要tableview在不边界回弹的情况下下拉改变整个view的frame
    if ([otherGestureRecognizer.view isKindOfClass:[UITableView class]] && self.tableview.contentOffset.y > 0) {
        
        return NO;
    }
    
    return YES;
}

gestureRecognizer的代理事件里边根据不同的需求分别处理

你可能感兴趣的:(uiscrollview(uitableview)和手势冲突解决)