3

ビューがあり、そのビューにUITapGesture. これで、ビューにボタンを配置してボタンをクリックしても、ボタン アクションが呼び出されません。

これが私のコードです:

    ButtionView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 84, 84)];
    ButtionView.tag=i;
    UIImageView *coverImageView = [[UIImageView alloc]     

    initWithFrame:CGRectMake(0,0,ButtionView.frame.size.width,84)];
    coverImageView.tag = i;
    UIButton *notesbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    notesbutton.frame=CGRectMake(0, 0,20,20);
    notesbutton.tag=i;

    [notesbutton addTarget:self action:@selector(buttonClickedForNotes:)      
    forControlEvents:UIControlEventTouchUpInside];
    [ButtionView addSubview:coverImageView];
    [ButtionView addSubview:notesbutton];
    [notesbutton bringSubviewToFront:ButtionView];
    [self.scrollview addSubview:ButtionView];
    [ButtionView addGestureRecognizer:oneFingerSingleTap];

-(IBAction)buttonClickedForNotes:(id) sender
{
    NSLog(@"buttion action call");

}
4

3 に答える 3

2

まず、UITapGestureのデリゲートにサブスクライブします。

[singleTapGestureRecognizer setDelegate:self];

次に、ボタンを現在のクラスのivarにする必要があります。次に、このメソッドをクラスに追加します。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // Check if the touch was on the button. If it was,
    // don't have the gesture recognizer intercept the touch
    if (CGRectContainsPoint(notesButton.frame, [touch locationInView:self.view]))
        return NO;
    return YES;       
}

これがお役に立てば幸いです。

于 2012-07-19T03:43:08.930 に答える
1

デフォルトでは、UIViewはユーザーイベントに応答できません。

ButtonViewを初期化した後にこのコードを使用して、userInteractionを有効にします。

ButtionView.userInteractionEnabled = YES;
于 2012-07-19T03:32:43.800 に答える
0

重要なことは何も変更していませんが、UITapGestureRecognizer メソッドが呼び出された後に Your Button アクション メソッドが呼び出されることを追加したいと思います。

ButtionView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 84, 84)];
ButtionView.tag=i;
UIImageView *coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,ButtionView.frame.size.width,84)];
coverImageView.tag = i;

UIButton *notesbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
notesbutton.frame=CGRectMake(0, 0,40,40);
notesbutton.tag=i;

[notesbutton addTarget:self action:@selector(buttonClickedForNotes:) forControlEvents:UIControlEventTouchUpInside];
[ButtionView addSubview:coverImageView];
[ButtionView addSubview:notesbutton];
[notesbutton bringSubviewToFront:ButtionView];
[self.view addSubview:ButtionView];
//[ButtionView addGestureRecognizer:oneFingerSingleTap];

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                         action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[ButtionView addGestureRecognizer:singleTapGestureRecognizer];
[singleTapGestureRecognizer release];
}

- (void)singleTap:(UITapGestureRecognizer *)gesture{
    NSLog(@"handle taps");
}

-(IBAction)buttonClickedForNotes:(id)sender{
    NSLog(@"buttion action call");
}
于 2012-07-02T07:16:18.860 に答える