4

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).

5
  • Are you using Interface Builder? If so, make sure you have your connections setup properly for the touches there.
    – bibo bode
    Dec 18, 2012 at 20:19
  • I don't see how that comment fits the scenario presented. If there were a problem with connections, there would always be a problem, not sometimes, right? In answer to your question, though, the main view controller was created in the storyboard using a subclass of UIViewController. It is this subclass .m file where all the code is. Also, what is there to connect? I also have a tap gesture recognizer added through the storyboard for double-tap, and it works properly. Dec 18, 2012 at 20:36
  • If you are now only receiving one touch at a time there is a good chance that multipleTouchEnabled is set to NO. You can set it to YES in either IB or code, in IB it's called "Multiple Touch".
    – NJones
    Dec 19, 2012 at 3:44
  • @NJones, I tried doing it in code, but it didn't work. So I checked the storyboard and looked for the field you suggested. It was not checked, so I checked it. That resolved the problem. So now my question is why didn't the code get called? I'm going to reread Apple's documentation tomorrow (er, later today) which I hope will answer this question. Dec 19, 2012 at 8:04
  • Interesting. It still didn't work, but for a different reason. The chain of points is different for the first and second touches. For the first touch, the current point always matches the previous point of the next step. For the second touch, that is true for all but the ending, which matches the previous point (and so my code didn't pick it up, e.g. last moved: {558.5, 817.5} --->> {558.5, 814.5} and ended: {558.5, 817.5} --->> {558.5, 814.5} for 2nd touch and moved: {175.5, 197.5} --->> {175.5, 199} and ended: {175.5, 199} --->> {176.5, 200.5} for the first touch. Dec 19, 2012 at 8:22

2 Answers 2

0

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.

0

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?

1
  • Interesting idea. I think it's a kludge and not a very good one for my particular application. The app is a multi-user, single device, fast-action game. Likely the two touch events are by competitors. In your scenario, the first player could squelch the second player by keeping their finger down. Not good. Edit: Maybe the same idea but after not hearing from a particular touch object, rather than an event. Dec 18, 2012 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.