繁体   English   中英

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

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

我有这个方案,它工作正常( --> =“具有子视图”):

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

正如预期的那样,该按钮将触发指定的选择器。
但是,如果我执行以下操作(出于某些原因,这是更好的选择),该按钮将停止接收触摸

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

有线索吗?

-更新

响应请求,下面是一些按钮创建代码:

// 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
}

最后, Controls.xib如下所示:

IB中的屏幕截图

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

[self.view bringSubviewToFront:<UIButton_INSTANCE>];

有时,当我在容器视图框架的外部设置控件元素时,这种情况发生了,对我来说也是如此。 我没有对此进行测试,但是由于您拥有许多嵌套视图,因此它可能会在第一个实例上发生,因为imageview可能足够大以包含UIButton。

由于没有足够的代码,因此无法精确找出发生这种情况的原因。

我想您应该尝试编写UIButton的类别来覆盖-touchesBegan:withEvent: -touchesMoved:withEvent: -touchesEnded:withEvent:并带有一些调试信息输出,例如:

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

当您按下UIButton实例并打印调试信息时,这意味着您的UIButton实例已收到触摸事件,但未正确处理。 相反,这意味着响应链中有一个实例阻止了事件的传递。

特别是,如果发现在手指移动后仅开始触摸的调试信息被打印,而不再移动已触摸的调试信息,则可能是手势识别器阻止了您的触摸事件。

暂无
暂无

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

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