0

何が間違っているのかわかりませんが、簡単な例を次に示します。

@interface Test : NSObject<UIGestureRecognizerDelegate> {
    UIView *_someParentView;
    UIView *_someChildView;
}
- (id)initWithParentView:(UIView *)parentView;
@end

@implementation Test

- (id)initWithParentView:(UIView *)parentView
{
    if (self = [super init])
    {
        _someParentView = parentView;
    }
    return self;
}

- (void)addSubViewsWhenReady
{
    _someChildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _someChildView.backgroundColor = [UIColor blackColor];
    [_someChildView setUserInteractionEnabled:YES];
    [_someParentView addSubview:_someChildView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.delegate = self;
    [_someChildView addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(id)sender
{
    NSLog(@"handle the single tap");
}

@end

出力: 「単一のタップを処理する」はログに記録されません。私が間違っていることについてのアイデアはありますか?

ありがとう!

4

2 に答える 2

3

あなたがやっているようにターゲットを設定します:

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

あなたの問題です。ここでの「self」が UIViewController であれば動作します。

すなわち

@interface Test : UIViewController<UIGestureRecognizerDelegate> {
    UIView *_someParentView;
    UIView *_someChildView;
}
- (id)initWithParentView:(UIView *)parentView;
@end

@implementation Test

- (id)initWithParentView:(UIView *)parentView
{
    if (self = [super init])
    {
        _someParentView = parentView;
    }
    return self;
}

- (void)addSubViewsWhenReady
{
    _someChildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _someChildView.backgroundColor = [UIColor blackColor];
    [_someChildView setUserInteractionEnabled:YES];
    [_someParentView addSubview:_someChildView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.delegate = self;
    [_someChildView addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(UIGestureRecognizer*)recognizer    {
    NSLog(@"handle the single tap");
}

@end
于 2013-12-30T00:24:36.060 に答える
1

の定義を変更してみてhandleSingleTap:ください

- (void)handleSingleTap:(UIGestureRecognizer*)recognizer {
    NSLog(@"handle the single tap");
}

UIGestureRecognizerドキュメントから:

ジェスチャ レコグナイザには、1 つ以上のターゲット アクション ペアが関連付けられています。ターゲットとアクションのペアが複数ある場合、それらは離散的で累積的ではありません。ジェスチャが認識されると、これらの各ペアのターゲットにアクション メッセージが送信されます。呼び出されるアクション メソッドは、次のシグネチャのいずれかに準拠する必要があります

- (void)handleGesture;

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

于 2013-04-14T00:41:36.000 に答える