UITapGestureRecognizer doesn't work added to an UIView?
Asked Answered
W

2

5

I'm triyng to make a gesture recognizer for a simple UIView:

UIView *theView = [[UIView alloc] initWithFrame:rect];
[theView setUserInteractionEnabled:YES];

UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                       action:@selector(handleTap)] autorelease];
[theView addGestureRecognizer:tap];

If I debug the property gestureRecognizers of the view it shows the gesture recognizer object. But when I tap inside the view it doesn't work.

The same code using an UIImageView works perfect, any ideas why doesn't work in UIView?

UPDATED:

An example class.

@implementation ExampleClass

- (UIView *)getViewInRect:(CGRect)rect
{
    UIView *theView = [[UIView alloc] initWithRect:rect];
    [theView setUserInteractionEnabled:YES];

    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
                                    initWithTarget:self 
                                    action:@selector(handleTap)] 
                                   autorelease];
    [aText addGestureRecognizer:tap];

    return theView;
}

- (UIImageView *)getImageViewInRect:(CGRect)rect
{
    UIImageView *theView = [[UIImageView alloc] initWithRect:rect];
    [theView setUserInteractionEnabled:YES];

    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
                                        initWithTarget:self 
                                                action:@selector(handleTap)] 
                                   autorelease];
    [theView addGestureRecognizer:tap];

    return [theView autorelease];    
}

- (void)handleTap
{
    NSLog(@"Tap Handled!!", nil);
}

@end

UPDATED 2:

Adding UITapGestureRecognizer to all subviews of theView don't fix the problem...

FIX IT!!!

OK!! The problem was the CGRect for theView, it had the width set to 0.0!!!

Winson answered 30/3, 2011 at 10:39 Comment(11)
what is your handleTap function looking like? Are you using a gestureRecognizer in the Superview of theView, that could catch the gesture?Limon
handleTap is a method of the same class where the above code live. From this scoop I can call it without problems with [self handleTap].Columbuscolumbyne
Are you saying that if you replaced UIView with UIImageview in the code snippet above it works?Scenography
That... shouldn't be. Show more code :)Scenography
@Scenography See the updated, is a C&P code from project (I can't show more). This appears a joke!! :-SColumbuscolumbyne
How are they added to your view hierarchy?Scenography
[someViewController.view addSubview:[exampleInstance getViewInRect:rect]]Columbuscolumbyne
This makes no sense :( Can you make a bare-bones project that reproduces this behaviour without giving anything about your project away? If you add the tap recognizer to self.view does THAT work?Scenography
I've created an isolated project only for this issue and works perfectly (including subviews). I have the problem in other part of the original project...but I don't know where. I will continue investigating... thanks to all!Columbuscolumbyne
Are you using a gestureRecognizer in the Superview of theView, that could catch the gesture?Limon
@Seega: No, I'm not using any type of gestureRecognizer or userInteractionEnabled in the superview...Columbuscolumbyne
U
19

Did you try this?

[view setUserInteractionEnabled:YES];
Undeviating answered 22/11, 2011 at 6:6 Comment(0)
U
2

Declare theview as ivar in .h file. Synthesize and then call it like this:

[self.theview setMultipleTouchEnabled:YES];

Don't forget to alloc and init that theview in viewDidLoad method.

That's it.

Uigur answered 30/3, 2011 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.