Touch on UISlider prevents scrolling of UIScrollView

前端 未结 5 1580
日久生厌
日久生厌 2021-02-20 10:28

I have a (vertical) UISlider inside a UIScrollview. I\'d like to be able to change the value of the slider, and, without lifting my finger, scroll the scrollview left or right.

5条回答
  •  礼貌的吻别
    2021-02-20 11:15

    You need to set delaysContentTouches as NO and prevent for UISlider objects to scroll, Check below code.

    mySlider.delaysContentTouches = NO;
    
    - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
        if ([view isKindOfClass:[UISlider class]])
        {
            UITouch *touchEvent = [[event allTouches] anyObject];
            CGPoint locationEvent = [touchEvent locationInView:view];
            CGRect thumbRect;
            UISlider *mySlide = (UISlider*) view;
            CGRect trackRect = [mySlide trackRectForBounds:mySlide.bounds];
            thumbRect = [mySlide thumbRectForBounds:mySlide.bounds trackRect:trackRect value:mySlide.value];
            if (CGRectContainsPoint(thumbRect, locationEvent))
                return YES;
        }
        return NO;
    }
    

提交回复
热议问题