【问题标题】:UIButton action is not called when UITapGesture in View视图中的 UITapGesture 时不调用 UIButton 操作
【发布时间】: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");

}

【问题讨论】:

标签: iphone objective-c uigesturerecognizer uitapgesturerecognizer


【解决方案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");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2015-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        相关资源
        最近更新 更多