5

シンプルな 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:

theView のすべてのサブビューに UITapGestureRecognizer を追加しても問題は解決しません...

修理する!!!

わかった!!問題は theView の CGRect で、幅が 0.0 に設定されていました!!!

4

2 に答える 2

19

これを試しましたか?

[view setUserInteractionEnabled:YES];
于 2011-11-22T06:06:56.730 に答える
2

ビューを .h ファイルで ivar として宣言します。合成してから、次のように呼び出します。

[self.theview setMultipleTouchEnabled:YES];

viewDidLoad メソッドでそのビューを割り当てて初期化することを忘れないでください。

それでおしまい。

于 2011-03-30T11:47:21.477 に答える