簡體   English   中英

UITapGestureRecognizer無法添加到UIView?

[英]UITapGestureRecognizer doesn't work added to an UIView?

我想為一個簡單的UIView做一個手勢識別器:

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

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

如果我調試視圖的屬性gestureRecognizers,它會顯示手勢識別器對象。 但是當我點擊視圖內部時,它不起作用。

使用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添加到View的所有子視圖不能解決問題...

修理它!!!

好!! 問題是CGRect for theView,它的寬度設置為0.0!

你試過這個嗎?

[view setUserInteractionEnabled:YES];

在.h文件中將視圖聲明為ivar。 合成然后像這樣調用它:

[self.theview setMultipleTouchEnabled:YES];

不要忘記在viewDidLoad方法中分配和初始化該視圖。

而已。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM