UISlider and UIScrollView

后端 未结 7 1820
猫巷女王i
猫巷女王i 2020-12-13 01:11

I have a UISlider as part of a view that is loaded into a UIScrollView with paging enabled. I\'ve noticed an unexpected behavior. If the user tries

7条回答
  •  自闭症患者
    2020-12-13 01:42

    You can subclass UIScrollView and override the method - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view as follows:

    - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
        if ([view isKindOfClass:[UISlider class]]) {
            UITouch *touch = [[event allTouches] anyObject];
            CGPoint location = [touch 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, location))
                return YES;
        }
        return NO;
    }
    

    Also Set yourScrollView.delaysContentTouches = NO; property for the scrollview.

提交回复
热议问题