views:

191

answers:

1

I'm wondering how to track touches anywhere on the iPhone screen and still have UIButtons respond to taps.

I subclassed a UIView, made it full screen and the highest view in the hierarchy, and overrode its pointInside:withEvent method. If I return YES, I'm able to track touches anywhere on the screen but the buttons don't respond (likely because the view is instructed to handle and terminate the touch). If I return NO, the touch passes through the view and the buttons respond, but I'm not able to track touches.

Do I need to subclass UIButton or is this possible through the responder chain? What am I doing wrong?

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return NO;
}   

//only works if pointInside:withEvent: returns YES.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"began");    
        [self.nextResponder touchesBegan:touches withEvent:event];
}

//only works if pointInside:withEvent: returns YES.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"end");      
        [self.nextResponder touchesEnded:touches withEvent:event];
}
+1  A: 

Rather then having extra view you can subclass your applications main window and track touches (and other events in it)

@interface MyAppWindow : UIWindow
...
@implementation MyAppWindow

- (void)sendEvent:(UIEvent*)event {
    [super sendEvent:event];

    if (event.type == UIEventTypeTouches){
         // Do something here
    }
   return;  
}

Then set your application window type to MyAppWindow (I did that in MainWindow.xib in IB)

Vladimir
Thanks, this is a great start. I've successfully began tracking touches in the window, but would like to pass the set of touches to the view controller to handle the touch events. Do I need to @class the view or something? The code, if anyone's interested: http://pastie.org/924485
Jonathan Cohen
Got the behavior I'm looking for. I added the views I want to the delegate, which adds them to the window on load. I made an @class of the view controller in the subclassed window, so I can control the view's properties when touch events are registered by the window.
Jonathan Cohen