Getting touches at launch on iOS
Asked Answered
D

3

8

On Mac, one can always get the location of the mouse "outside the event stream" (ie, even if you've not subscribed to any delegate methods for mouseUp: et al) by calling [NSEvent mouseLocation].

Is there any way on iOS to get current touch events without listening to touchesBegan:?

I ask because there is at least one situation in which touchesBegan is not called: at app launch, if a touch is already in progress, touchesBegan is never called. In fact, neither are any of the touch-related UIEvent methods called as near as I can tell (nor UIApplication's / UIWindow's sendEvent:, apparently).

I would like to vary the behavior of my app slightly based on whether a touch is in progress at launch. Is there any way to detect an in-progress touch at app launch?

Domineer answered 12/8, 2013 at 17:38 Comment(5)
What does "at app launch" mean for you application? Do you do any time-consuming actions on the main thread at the load time?Frippery
"at app launch" is "when the user taps the app icon on the springboard and the app opens". Not getting touch events is not because the app is unduly burdened with heavy processing; it's because touches which are in progress when the app opens are not delivered to the app via the normal means. I'm trying to find out if anyone knows a way outside the normal loop, perhaps subclassing and overriding, to get touches which are in progress arbitrarily, from any method, at any time.Domineer
I think that this is not possible. UIApplication is not suitable for subclassing and that is the entry point for your applicationArticulate
Understood. You want to get touches that have been started at springboard and continues while you app is launching. I don't believe it is possible.Frippery
UIApplication is easy subclassed. If there were a method which were being called on UIApplication for touches already in progress at launch, I would go that route. Do you know of one?Domineer
B
6

This cannot be done. The simple reason: The touch events don't belong to your app. Each touch belongs to some UI element (or responder). As you already know, this element gets the began, moved, ended, cancelled messages.

This is even true within a properly programmed app: All events regarding one touch are delivered to the very same object. After all, how would another object know what to do with that event, and how should the first object properly finish its expected behavior?

While you can (or could, but probably shouldn't) find a work around within your app, there's just no way for cross-app-touch passings.

And on the Mac you may query the mouse position, but in normal application flow there'll always be a mouse down before you get a mouse up event.

To be honest, I don't see any reason why this would be needed anyway... oh wait... I could split my app icon into several areas... not sure if it would already break privacy laws, though, if you get to know where the user has his icon on screen.

Balm answered 20/8, 2013 at 16:14 Comment(1)
Thanks for the time put into the answer, and the clarification. While this seems to be the current canonical answer, I feel like I want to wait until there is an "answer" to this question - eg, in iOS 9 after my bug report is addressed ;) I think I can change answers later though so...Domineer
A
1

I think you could simply "extend" the application launch. When I had time consuming tasks during my application launch, I used to show the same splash screen with a UIActivityIndicator while the action was being carried out.

You could simply create a NSTimer, wait for about 2 seconds and during this time, check for touches, while the splash screen will still be showing.

To do this, in applicationDidFinishLaunch, push a ViewController that looks exactaly like the splash screen and check for touches in this ViewController. After those 2 seconds, proceed with normal initialisation. This behaviour also helps if you have time consuming tasks during initialisation.

I know, it`s a workaround, but my guess, is that it is not possible to check for touches because application will be working on the main thread and the touches also processes on the main thread. This could happen also because there are no ViewControllers or UIWindow initialised and ready to listen to touches.

Hope it helps.

Articulate answered 12/8, 2013 at 18:47 Comment(0)
A
-2

You might try handling the hitTest:withEvent: instead.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 

Since according to apple doc "Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point."

Almuce answered 16/8, 2013 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.