Gesture problem: UISwipeGestureRecognizer + UISlider

前端 未结 3 1481
温柔的废话
温柔的废话 2020-12-01 03:41

Got a gesture related problem. I implemented UISwipeGestureRecognizer to get swipe left and right events and that is working fine. However the problem I\'m facing is that th

相关标签:
3条回答
  • 2020-12-01 04:15

    Swift 4.0 version. Don't forget the UIGestureRecognizerDelegate.

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    
        if let touchedView = touch.view, touchedView.isKind(of: UISlider.self) {
            return false
        }
    
        return true
    }
    
    0 讨论(0)
  • 2020-12-01 04:18

    The simplest way to handle this is probably to prevent the gesture recognizer from seeing touches on your slider. You can do that by setting yourself as the delegate, and then implementing

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        if ([touch.view isKindOfClass:[UISlider class]]) {
            // prevent recognizing touches on the slider
            return NO;
        }
        return YES;
    }
    

    If this doesn't work, it's possible the slider actually has subviews that receive the touch, so you could walk up the superview chain, testing each view along the way.

    0 讨论(0)
  • 2020-12-01 04:18

    I ended up getting this working just before Lily responded above. Here is the code I used, but Lily's looks cleaner (haven't tested Lily's thou):

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        BOOL AllowSwipes = YES;
    
            CGPoint point1 = [touch locationInView:_sliderLeft];
            CGPoint point2 = [touch locationInView:_sliderRight];
    
            //Left slider test
            if ([_sliderLeft hitTest:point1 withEvent:nil] != nil) {
                AllowSwipes = NO;
                NSLog(@"On Left Slider");
            }
    
            //Right slider test
            if ([_sliderRight hitTest:point2 withEvent:nil] != nil) {
                AllowSwipes = NO;
                NSLog(@"On Right Slider");
            }
        }
        return AllowSwipes;
    }
    
    0 讨论(0)
提交回复
热议问题