Question

My iPhone game's appDelegate has a "UIWindow window" member variable. I add UIImageViews to the window and move them around during game play. I'd like to handle touches on the UIWindow. How can I do this? Do I have to subclass UIWindow?

I tried adding the touchesBegan:withEvent and touchesEnded:withEvent methods to the window but they are not called (possibly due to the window being covered by UIImageViews... for example the game background is a UIImageView and it covers the entire screen).

Cheers!

Was it helpful?

Solution

Yes, if the UIImageViews cover the window and they accept touches the touches will not get passed through. You can place code like this in your UIImageView's touchesBegan to pass touches through to the next object in the layers of objects.

  [self.nextResponder touchesBegan:touches withEvent:event];

Having gone down this road before (have the window or a main UIView control a bunch of things in my game) I would encourage you to think about having each UIImageView capture touch events and deal with them. The UIImageView can then pass information along to other objects as needed.

Apple's documentation for UIWindow and the View Programming Guide seem to suggest that you should try to keep code outside of the window and place it into views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top