iOS - UIViews没有响应触摸事件

时间:2012-10-04 16:00:01

标签: iphone ios uiview uiscrollview event-handling

我正在尝试回应一些UIViews上的触摸事件。以下是视图控制器在IB中的显示方式:

enter image description here

这是视图层次结构:

enter image description here

我已经实现了以下四种“触摸”方法:

- (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{

}

无论我在屏幕上的哪个位置触摸,都不会发送事件。我在“touchesBegan”的开头有一个断点,这个代码永远不会到达。

所有视图都选中了“启用了用户交互”,并且已正确连接到其商店。

我错过了什么?在我的项目的其他地方,我正在做同样的事情,它工作正常。

是否与我的根视图是UIScrollView?

有关

谢谢!

编辑:我已按照以下建议操作,并将UIScrollView子类化,以便将触摸发送到NextResponder。看来我现在正在接触,但是,视图的背景颜色是否实际变化是不正确的。

例如,如果我快速点击视图,则会触发“触摸”方法,但背景颜色不会改变。但是,如果我按住并按住约一秒钟,背景颜色会按照我的预期变化。什么可能导致这种奇怪的行为?

编辑:添加行:

[scrollView setDelaysContentTouches:NO];

到我的viewDidLoad方法为我解决了这个问题。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您是对的,您的问题是您的视图放在UIScrollView中。滚动视图获取所有触摸事件,但不会将它们传递给其子视图。

您可以获得此问题的解决方案: