开发者

UITapGestureRecognizer doesn't work added to an UIView?

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];

开发者_JS百科    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!!!


Did you try this?

[view setUserInteractionEnabled:YES];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜