views:

213

answers:

2

I have a UI element. When I tap it, it animates to an intermediary state. A tap anywhere else on the screen should cancel the interaction, while another tap on the UI element should proceed.

I've implemented this using a guard view (subview of window & above every other subview) and then reparenting the element into the guard view before the animation, but I'm curious as to what other ways people can think of doing this, that might be cleaner.

I've thought of using a guard window instead of a view, and messing with hitTest:withEvent: and pointInside:forEvent:. The problem with those two is that the superview of the element does not take up the whole screen.

This sounds like a perfect use case for what exclusiveTouch sounds like (all touch events anywhere go to that view), but in reality isn't (all touch events started in that view only go to that view, and not other views at the same time).

Thoughts?

+2  A: 

You can create another UIWindow, call -makeKeyAndVisible and then destroy the window when you're done with it. UIAlertView does exactly this to take control of the entire screen.

rpetrich
This still involves that ugly reparenting code, and an extra window ivar. Really curious if there's a different way of doing it.That said this is probably what I'll end up going with.
Colin Barrett
Yeah, it's ugly, but it's the only method I know of that works properly (adding to the current window doesn't intercept the "scroll to top" and "navigation view button" taps on the statusbar). It's ripe for abstracting into a separate class though :)
rpetrich
+1  A: 

I've written some code that achieves this behavior fairly cleanly. You don't need to reparent the element into the guard view. You just need a UIControl subclass that can pass touches through to the element. You also don't need to destroy the guard window; you can just hide it. Your UIApplicationDelegate can listen for UIWindowDidBecomeHiddenNotifications that are sent by UIWindows that aren't the main window, and then call the main window's -makeKeyAndVisible method.

lemnar
That's quite cool! Thanks!
Colin Barrett