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 方法之后被调用。

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 回答