5
votes

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!!!

2
what is your handleTap function looking like? Are you using a gestureRecognizer in the Superview of theView, that could catch the gesture?Seega
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].Víctor B.
Are you saying that if you replaced UIView with UIImageview in the code snippet above it works?freespace
That... shouldn't be. Show more code :)freespace
@freespace See the updated, is a C&P code from project (I can't show more). This appears a joke!! :-SVíctor B.

2 Answers

19
votes

Did you try this?

[view setUserInteractionEnabled:YES];
2
votes

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.