简体   繁体   English

iOS-UIViews不响应触摸事件

[英]iOS - UIViews not responding to touch events

I am trying to respond to touch events on some UIViews. 我正在尝试响应某些UIView上的触摸事件。 Here's how the view controller looks in IB: 这是视图控制器在IB中的外观:

在此处输入图片说明

Here's the view hierarchy: 这是视图层次结构:

在此处输入图片说明

I have implemented all four "touches" methods as follows: 我已经实现了所有四个“接触”方法,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if(touch.view==locPhoneView){
        locPhoneView.backgroundColor=[ColorUtility colorWithHexString:@"83d3f0"];
    }
    if(touch.view==locAddressView){
        locAddressView.backgroundColor=[ColorUtility colorWithHexString:@"83d3f0"];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if(touch.view==locPhoneView){
        locPhoneView.backgroundColor=[UIColor whiteColor];
    }
    if(touch.view==locAddressView){
        locAddressView.backgroundColor=[UIColor whiteColor];
    }

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

}

No matter where I touch on the screen, the events are not sent. 无论我在屏幕上的何处触摸,都不会发送事件。 I have a breakpoint at the beginning of "touchesBegan", and this code is never reached. 我在“ touchesBegan”的开头有一个断点,但是从未达到此代码。

All the views have "User interaction enabled" checked, and are hooked up appropriately to their outlets. 所有视图都选中了“已启用用户交互”,并已适当地连接到它们的出口。

What am I missing? 我想念什么? Elsewhere in my project I'm doing the same thing and it works fine. 在我项目的其他地方,我正在做同样的事情,而且效果很好。

Could it be related to the fact that my root view is a UIScrollView? 可能与我的根视图是UIScrollView有关吗?

Thanks! 谢谢!

EDIT: I have followed the advice below, and subclassed UIScrollView so that the touches are sent to NextResponder. 编辑:我已经按照下面的建议,并子类化UIScrollView,以便将触摸发送到NextResponder。 It appears that I'm getting the touches now, however, whether the background color of the view actually changes is not behaving correctly. 看来我现在正在摸索,但是,视图的背景颜色是否实际上发生了变化,这并不是正确的行为。

For example, if I do a quick tap on a view, the "touches" methods fire, but the background color does not change. 例如,如果我快速点击视图,则会触发“ touches”方法,但背景颜色不会改变。 However, if I tap and hold for about a second, the background color changes as I expect it to. 但是,如果我点击并按住约一秒钟,则背景颜色会按我预期的那样变化。 Any idea what could be causing this strange behavior? 任何想法可能导致这种奇怪的行为吗?

EDIT: Adding the line: 编辑:添加行:

[scrollView setDelaysContentTouches:NO];

to my viewDidLoad method solved this problem for me. 我的viewDidLoad方法为我解决了这个问题。 Thanks for your help! 谢谢你的帮助!

You are right, your problem is that your views are placed in a UIScrollView. 没错,您的问题是您的视图放置在UIScrollView中。 The scroll view is getting all the touch events and does not pass them to its subviews. 滚动视图将获取所有触摸事件,并且不会将它们传递到其子视图。

You can get solutions for this problem: 您可以获得此问题的解决方案:

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

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