【问题标题】:Detect a touch on a subview in a main controller检测主控制器中子视图的触摸
【发布时间】:2013-05-08 06:02:39
【问题描述】:

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

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

【问题讨论】:

  • 只需给每个按钮一个唯一的标签,点击就可以得到你点击的按钮
  • 带有 UIButton 的卡片封面?你什么意思?

标签: cocoa-touch uibutton view-hierarchy


【解决方案1】:

在你所有的按钮setExclusiveTouch 上。如:

[button setExclusiveTouch:YES];

更多细节可以参考:

  1. exclusiveTouch
  2. Specifying Custom Touch Event Behavior

【讨论】:

    【解决方案2】:

    我部分理解你的问题。看看以下是否有帮助:

    在您的滚动视图中:

     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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多