I am trying to respond to touch events on some UIViews. Here's how the view controller looks in 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.
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?
Thanks!
EDIT: I have followed the advice below, and subclassed UIScrollView so that the touches are sent to 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. 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. Thanks for your help!