Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView contents not allowing user interaction

I have a UIScrollView with paging enabled like the following:

container = [[UIScrollView alloc] initWithFrame:kScrollViewFrame];
[container setDelegate:self];
[container setShowsHorizontalScrollIndicator:YES];
[container setShowsVerticalScrollIndicator:NO];
[container setClipsToBounds:YES];
[container setPagingEnabled:YES];
[container setDecelerationRate:UIScrollViewDecelerationRateFast];
[container setBounces:NO];
[container setUserInteractionEnabled:NO];
[container setCanCancelContentTouches:NO];
[container setDelaysContentTouches:NO];

To the UIScrollView, I add several UIWebViews, and set their interaction enabled to yes like so.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.frame = frame;
        self.userInteractionEnabled = YES;
    }
    return self;
}

which breaks paging and all touches on the UIScrollView. If I set user interaction enabled to NO, page works, but I can't highlight text in the UIWebView. I tried subclassing UIScrollView like the following, but the same circumstances occur. Any idea?

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"init");
    return [super initWithFrame:frame];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");

    [[self nextResponder] touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");

    [[self nextResponder] touchesMoved:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
    [[self nextResponder] touchesEnded:touches withEvent:event];
}
like image 766
Oh Danny Boy Avatar asked Apr 02 '12 14:04

Oh Danny Boy


1 Answers

Disabling user interaction on the container effectively disables it on the subviews as well. You should enable it, causing the touch event to propagate down the view hierarchy;

[container setUserInteractionEnabled:YES];

If you want to disable scrolling on the UIWebView just get inside its UIScrollView

yourWebView.scrollView.scrollEnabled = NO;

I've tested this on my device and I can both "page through" the UIScrollView and select text on UIWebView.

EDIT:

I got this working! You have to allow touch canceling as well:

[container setCanCancelContentTouches:YES];
like image 199
Bartosz Ciechanowski Avatar answered Sep 23 '22 18:09

Bartosz Ciechanowski