UITapGestureRecognizer не работает добавлен в UIView? - PullRequest
5 голосов
/ 30 марта 2011

Я хочу создать распознаватель жестов для простого UIView:

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

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

Если я отлаживаю свойство gestRecognizer представления, оно показывает объект распознавателя жестов. Но когда я нажимаю на изображение, оно не работает.

Тот же код, использующий UIImageView, отлично работает, есть идеи, почему не работает в UIView?

ОБНОВЛЕНИЕ:

Пример класса.

@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

ОБНОВЛЕНО 2:

Добавление UITapGestureRecognizer во все подпредставления theView не решает проблему ...

ИСПРАВИТЬ ЭТО !!!

OK !! Проблема была в CGRect для theView, ширина которого была равна 0.0 !!!

Ответы [ 2 ]

19 голосов
/ 22 ноября 2011

Вы пробовали это?

[view setUserInteractionEnabled:YES];
2 голосов
/ 30 марта 2011

Объявите представление как ivar в .h файле. Синтезируйте, а затем назовите это так:

[self.theview setMultipleTouchEnabled:YES];

Не забудьте выделить и инициализировать это представление в методе viewDidLoad.

Вот и все.

...