シンプルな 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 に設定されていました!!!