MonkeyBlue
MonkeyBlue

Reputation: 2234

UIPickerView inside UIScrollView hitTest to disable Scrollview?

I am currently building an application which has a resized UIPickerView on a scrollview the problem I am facing is that when you try to scroll the picker the scrollview is moving instead.

For the past few hours I have tried to fix this by disabling the scrollview when the picker is selected by creating a UIScrollView sub class which performs the following hitTest:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];
    if ([result.superview isKindOfClass:[UIPickerView class]])
    {
        NSLog(@"Cancel touch");
        self.canCancelContentTouches = NO;  
        self.delaysContentTouches = NO;
        self.scrollEnabled = NO;
    }
    else 
    {
        self.canCancelContentTouches = YES; 
        self.delaysContentTouches = YES; 
        self.scrollEnabled = YES;
    }
    return result;
}

With this above code I find that when I click and hold certain sections of the UIPickerView it cancels the touch and disables the UIScrollview OK and I can move the UIPicker to select a new value, however when I certain areas on the PickerView and do an NSLog on the reported class like below

NSLog(@"%@", [result.superview class]);

It outputs UIPickerTableViewWrapperCell to the console and the isKindOfClass[UIPickerView class] never gets entered.

I have also tried isMemberOfClass which also does not work.

Any help would be appreciated.

Thanks Aaron

Upvotes: 3

Views: 2252

Answers (1)

MonkeyBlue
MonkeyBlue

Reputation: 2234

I think I have figured this out, by searching the class description to see if it contains @"UIPicker*"

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{

    UIView* result = [super hitTest:point withEvent:event];

    NSString *viewDescription = [result.superview class].description;
    NSRange range = [viewDescription rangeOfString : @"UIPicker"];

    if (range.location != NSNotFound) 
    {
        NSLog(@"Cancel touch on ScrollView");
        self.canCancelContentTouches = NO;  
        self.delaysContentTouches = NO;
        self.scrollEnabled = NO;
    }
    else 
    {
        self.canCancelContentTouches = YES; 
        self.delaysContentTouches = YES;
        self.scrollEnabled = YES;
    }
    return result;
}

Now when I drag the UIPicker it works perfectly and the scrollview does not move at all, unless I am clicking on that and dragging it.

Aaron

Upvotes: 4

Related Questions