检测主控制器中子视图的触摸

时间:2013-05-14 04:14:53

标签: cocoa-touch uibutton view-hierarchy

我有一个主视图控制器类,其中包含UIScrollView和许多子视图,如卡片。

每张卡片都是一个对象,它被UIButton覆盖。我想检测UIButton上的点按,并希望一次禁止多张卡上的点击。

2 个答案:

答案 0 :(得分:0)

我理解你的问题。看看是否有帮助:

在滚动视图中:

 for (int i=0;i<array;i++)
 {
       UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(scrollWidth, 5,50,40)];
       button.userInteractionEnabled=YES;
       UITapGestureRecognizer *rcognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonSelcted:)];
       [button addGestureRecognizer:rcognizer];
       [scrollView addSubview:button];
       scrollWidth=scrollWidth+80;
  }

在buttonSelected方法中,只需执行以下操作:

-(void)buttonSelected:(UITapGestureRecognizer *)recognizer
{
    UIButton *selectedItem=(UIButton*)recognizer.view;
    //do what you want with button
}

答案 1 :(得分:0)

在所有按钮setExclusiveTouch上。为:

[button setExclusiveTouch:YES];

有关详细信息,请参阅:

  1. exclusiveTouch
  2. Specifying Custom Touch Event Behavior