简体   繁体   English

添加到self.view时,UIButton没有接触

[英]UIButton not getting touches when added to self.view

I have this scheme which is working fine ( --> = "has subview"): 我有这个方案,它工作正常( --> =“具有子视图”):

ViewController.view --> imageView --> containerView --> UIButton

As expected, the button fires off the designated selector. 正如预期的那样,该按钮将触发指定的选择器。
However, if I do the following (which for several reasons is preferable) the button stops receiving touches . 但是,如果我执行以下操作(出于某些原因,这是更好的选择),该按钮将停止接收触摸

ViewController.view --> imageView
ViewController.view --> containerView --> UIButton
[self.view bringSubviewToFront:_containerView]; 

Any leads? 有线索吗?

-- UPDATE -更新

Responding to requests, here is some button creation code: 响应请求,下面是一些按钮创建代码:

// in viewDidLoad

_controlsView = [[[NSBundle mainBundle] loadNibNamed:@"Controls" 
       owner:self options:nil] objectAtIndex:0];
_controlsView.frame = CGRectMake(0, 50, 
      _controlsView.bounds.size.width, _controlsView.bounds.size.height);
[self.view addSubview:_controlsView];
// ...
[_controlsView.superview bringSubviewToFront:_controlsView];

// in viewDidAppear

if (!_demoButton) {
   _demoButton = (UIButton*)[_controlsView viewWithTag:kDemoButtonTag];
   [_demoButton addTarget:self action:@selector(buttonPressed:) 
       forControlEvents:UIControlEventTouchUpInside];
   // configuration, color, font, layer, etc. all works well
}

Finally, the Controls.xib looks like this: 最后, Controls.xib如下所示:

IB中的屏幕截图

Probably您的观点支配着您的按钮。

[self.view bringSubviewToFront:<UIButton_INSTANCE>];

Sometimes this happens, well has happened to me, when I have set a control element outside the frame of the container view. 有时,当我在容器视图框架的外部设置控件元素时,这种情况发生了,对我来说也是如此。 I've not tested this, but as you've got many nested views, it may occur on the first instance as the imageview may be large enough to encompass the UIButton. 我没有对此进行测试,但是由于您拥有许多嵌套视图,因此它可能会在第一个实例上发生,因为imageview可能足够大以包含UIButton。

Due to not enough codes, it is impossible to precisely figure out the reason why this happened. 由于没有足够的代码,因此无法精确找出发生这种情况的原因。

I suppose you should try to write a category of UIButton to overwrite -touchesBegan:withEvent: -touchesMoved:withEvent: -touchesEnded:withEvent: with some debug info output like: 我想您应该尝试编写UIButton的类别来覆盖-touchesBegan:withEvent: -touchesMoved:withEvent: -touchesEnded:withEvent:并带有一些调试信息输出,例如:

{
    [super touchesBegan:touches withEvent:event];
    NSLog(@"Touches began/moved/ended!");
}

When you pressed the UIButton instance, and the debug info was get printed, that means your UIButton's instance has received touch events but not handled it in right way. 当您按下UIButton实例并打印调试信息时,这意味着您的UIButton实例已收到触摸事件,但未正确处理。 And in the opposite, that means there is an instance in respond chain blocked the events delivery. 相反,这意味着响应链中有一个实例阻止了事件的传递。

Particularly, if it is found that only touches began debug info get printed and touches moved debug info no longer get printed after your fingers moved, your touch events probably were blocked by a gesture recognizer. 特别是,如果发现在手指移动后仅开始触摸的调试信息被打印,而不再移动已触摸的调试信息,则可能是手势识别器阻止了您的触摸事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM