视图中的 UITapGesture 时不调用 UIButton 操作

Posted

技术标签:

【中文标题】视图中的 UITapGesture 时不调用 UIButton 操作【英文标题】:UIButton action is not called when UITapGesture in View 【发布时间】:2012-06-30 06:19:44 【问题描述】:

我有一个视图,并在该视图上添加了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");


【问题讨论】:

看这个问题,应该是你的问题***.com/questions/4825199/… 【参考方案1】:

首先,订阅 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;       

希望这会有所帮助。

【讨论】:

【参考方案2】:

默认情况下,UIView 无法响应用户事件。

在初始化ButtonView之后使用这段代码来启用它的userInteraction:

ButtionView.userInteractionEnabled = YES;

【讨论】:

【参考方案3】:

没有改变任何重要的东西。但是想追加你的按钮操作方法将在 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");

【讨论】:

以上是关于视图中的 UITapGesture 时不调用 UIButton 操作的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UITableViewCell 的子视图禁用 UITapGesture?

UITableview 标头中的 UITapGesture

MVC模式窗口中的jQuery UI自动完成

在水平堆栈视图 + 元素的全宽上注册 UITapGesture

导航到模块中的视图时不调用 OnNavigatedTo

触发 UITapGesture 时从单元格的子视图中获取单元格的引用