iOS UIPickerView:如何限制滚动范围

iOS UIPickerView: How to limit the scrolling range

当您滚动 UIPickerView 时,您可以在触摸板上上下移动手指 - 根据屏幕的大小上下移动。有没有办法限制这个上下滚动的范围到UIPickerView的区域?这样一来,一旦您将手指移出 UIPickerViewUIPickerView 的滚动就会停止?我正在制作 UIPIckerView 作为游戏中的旋转器,为了公平游戏,我需要实现这一点。

你可以使用 UITapGestureRecognizer 和 UIPickerView 然后你可以得到坐标。如果你在你想要的框架中得到点,那么就做你的工作,否则 return

//viewDidLoad

UILongPressGestureRecognizer* gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pickerLongPressDetected:)];
    gestureRecognizer.minimumPressDuration = .001;
    gestureRecognizer.cancelsTouchesInView = NO;
    gestureRecognizer.delegate = self;
    [self.rollerView addGestureRecognizer:gestureRecognizer];

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    // return
    return true;
}

-(void)pickerLongPressDetected:(UILongPressGestureRecognizer *)gesture
{
    CGPoint touchPoint = [gesture locationInView:self.view];

    CGRect frame = self.rollerView.frame;

    if(!CGRectContainsPoint(frame, touchPoint))
    {
        //methods for when you scroll outside of your picker view's frame
    }
}