touchesEnded not getting called after device changes orientation - ios

I am implementing touchesBegan and touchesEnded in my iOS application trying to detect when the user puts a finger on the screen and when he releases it.
The problem I am having is that as soon as touchesBegan gets called, if the user rotates the device while still holding his finger on the screen, when he lets go of the screen, touchesEnded does not get called.
Does anyone know why this might be happening?

Are you getting touchesCancelled instead?
In general, the system will call either touchesEnded or touchesCancelled after a touchesBegan, so code should deal with both. Touches can be cancelled for various reasons, such as a gesture recognizer taking over, an non-interactive animation starting on the view, an incoming phone call, etc.

Related

TouchesMoved stops firing for no reason

I added a custom gesture recognizer to a ViewController's view. Once a touch starts, the custom class reports the touch.force in NSLog every time the touchesMoved function fires off.
If you start touching and the move your finger even just a few millimeters, then the touchesMoved events will fire off continually until you lift up your finger and the touchesEnded event fires.
However if you start touching and DON'T move your finger at all, then the touchesMoved events will only fire off a few times, maybe 10-20 times, and then they just stop. After that, the touchesCancelled function does not get called and neither does touchesEnded.
Why is this happening? I want it to keep firing touchesMoved forever until the finger is lifted up.
I saw this other similar question here: iOS app stops responding to touch events until I move my finger on/off screen
But there is no answer to that one either.

iOS layer color change

For my custom UIView I've overriden touchesBegan method. What I told it to do is to change its' layer's background color:
dispatch_async(dispatch_get_main_queue()){
self.layer.backgroundColor = clr_someCGColor
}
It acts weird. If I quickly tap the view while in Landscape it does everything perfectly well, but if I do this in Portrait, I have to hold it for some time to see the result, however the touchesEnded method is called right away, if I quickly tap. What could be the reason, causing the delay in Portrait?
Remove the dispatch_async wrapper. All it does is cause a delay (we can't execute on the main thread until, as you rightly say, the tap ends and touchesEnded has come and gone). You are already on the main thread, in touchesBegan, so there is no need for this extra delay.
Even better, use a tap gesture recognizer.

touchesEnded or touchesCancelled not always called

This issue I think deserves its own question. Using the code attached to my solution to another problem I discovered the issue described here.
I have the main view controller set as a UIGestureRecognizerDelegate, and I implement touchesBegan, touchesMoved, touchesEnded, and touchesCancelled programming my solution with the assumption that for every touch object with a touchesBegan event there would be a touchesEnded or touchesCancelled event for that same object. I'm finding that not to be the case, though.
Scenario:
The following events happen in this order.
User starts gesture 1, touching the screen and sliding the finger.
User starts gesture 2, touching the screen at a different location.
User continues to slide both fingers at their respective parts of the screen.
User lifts finger off the screen for gesture 2.
User continues gesture 1.
User lifts finger off the screen for gesture 1.
Using NSLog to capture the details of the touch event, I find that a separate touch object is used for gesture 1 and gesture 2. But while touchesBegan, touchesMoved, and touchesEnded are all called for gesture 1, only touchesBegan and touchesMoved are called for gesture 2. The event touchesCancelled is not called for it either.
So how can I tell when gesture 2 finishes if touchesEnded and touchesCancelled are not called?
Edit: I found another post with similar symptoms. Most of my subviews are created programmatically, though. I'll try what was suggested there for the others. I'm skeptical it is the same issue, though, since in my testing, the touch locations are not anywhere near the other views.
Another edit: Following the recommendation in the link posted in my previous edit, I looked at the subviews, and one had user interaction checked. After I unchecked it, the behavior is slightly different. Now the second touch isn't noted at all in any of the touch events. I must be missing something basic. The main view, and the view with user interaction checked, by the way, both occupy the same space (one encapsulates the other).
My original assumption that each touch would have its own object that starts at touchesBegan and ends with either touchesEnded or touchesCancelled I think is correct. It is with my current implementation anyway. I originally wasn't seeing a second touch because Multiple Touch was not enabled for the view I was working with. I enabled that, per suggestion in the comments. After that, I was able to see some, but not all touch events for the second touch. The reason I was sometimes not seeing the second touch was because I had a subview that had user interaction enabled. Apparently, it was commandeering the touches. I unchecked that and then was able to see the touch objects.
I then switched tracking the touches by coordinates to touch IDs and was able to track the complete lifespan of all touches. Tracking by coordinates didn't work because I found that for the second touch, the touchesEnded coordinates were identical to the last one in touchesMoved rather than the previous location in touchesEnded matching the touchLocation in touchesMoved as with the first touch. If this sounds confusing, just track the touches by touch ID instead of by coordinates.
How about you put something like this in your touchesMoved method
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray* touchData = #[touches,event];
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:#selector(touchesFinishedWithoutCallback:) userInfo:touchData
repeats:NO];
[self.timer fire];
}
The touchesFinishedWithoutCallback: method will only get called when touchesMoved stops getting called.
Needs elaborating for multiple touch, but could be a solution?

Touch Events touchesCancelled and touchesEnded

I have a project that I started out using a tap gesture recognizer for. I realized I didn't have enough control with the tap gesture recognizer, so I've started coding with using my viewcontroller as a UIGestureRecognizerDelegate. Just to make sure I was on the right track, I added methods for touchesBegan, touchesMoved, touchesEnded, touchesCancelled. The methods are empty except for NSLog calls so I can tell what is being fired when I try different things.
Things worked as expected except that I was getting a bunch of calls to touchesCancelled. I assume this is because of the tap gesture recognizer I still have in place. I'm not ready to remove the tap gesture recognizer, so I just wanted to confirm that this is what would happen if a gesture I used was actually a tap.
The documentation says:
This method is invoked when the Cocoa Touch framework receives a
system interruption requiring cancellation of the touch event; for
this, it generates a UITouch object with a phase of
UITouchPhaseCancel. The interruption is something that might cause the
application to be no longer active or the view to be removed from the
window When an object receives a touchesCancelled:withEvent: message
it should clean up any state information that was established in its
touchesBegan:withEvent: implementation.
But I suspect my scenario just outlined is just as likely. Am I correct?

a last touch event does not come if the view goes out of the screen programmatically

I have a situation where I apply an effect to a UIView when a touch begins and reverse that effect when that touch ends. So basically I am tracking touchesbegan, touchesEnded and touchesCancelled methods of UIView.
But the problem is that when the view goes out of the screen, i.e. when it or one of its parents gets removed from superview, it does not get any more touch events. Is there any way to give this "last" touchesended event to the view? Maybe if the UIView gets notified about being invisible, I can also use this event for that purpose.
Ok I am going to move the answers in comments to original question to make a good summary of important points.
The reason I am tracking touch events is that I want to apply some
nice effects such as glowing on touch start and remove those effects
on touch ending.
The reason why I can not simulate touchesEnded on removing those
views is that I do not directly remove them. Instead I remove one of
the ancestor views of them. I can not keep track of ancestor views
all the way to UIWindow, it is technically impossible I think.
Instead, framework should provide this to as an event I think.
I solved my problem by overriding -(void)willMoveToWindow:(UIWindow *)newWindow method and checking if newWindow is nil.

Resources