Frage

In my app I need to catch the exact number of finger on the screen, I try two ways but I have 2 different problem.

First way:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
    int count = [allTouches count];
    NSLog(@"number of touch:%d", count);
}

this give me a NOT accurate number of touches if I use more finger at the same time.

Second way:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    int num = [touches count];
    totalTouch = totalTouch+num;
    NSLog(@"number of touch:%d", totalTouch);
}

in this way I use a global var (totalTouch) that I increment everytime touchbegan is called and I have a perfect number of touches. Naturally i set at '0' this var in the touchend

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    totalTouch = 0;
}

My problem is that, with the second way I do a control in touchbegan, this:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    int num = [touches count];
    totalTouch = totalTouch+num;

    if (totalTouch == numberToVerify){
       //IT ENTER HERE EVERYTIME INSIDE THIS IF
    }
    else{
    }
 }

so everytime it enter inside if-conditions, and I don't want it, I want do this control only when I have a final number of touch...

War es hilfreich?

Lösung

Within your

 - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     int num = [touches count];
     totalTouch = totalTouch+num;
     NSLog(@"number of touch:%d", totalTouch);
  } 

You can get the number of fingers on the screen with

  [[event allTouches]count]

This can be accessed from - (void)touchesBegan: - (void)touchesEnded: or - (void)touchesMoved:

Andere Tipps

In touchesBegan:withEvent:, the touches argument only contains touches in the “begin” phase (UITouchPhaseBegan). In touchesEnded:withEvent:, the touches argument only contains touches in the “end” phase (UITouchPhaseEnded). Similarly for touchesMoved:withEvent: and touchesCancelled:withEvent:.

If you want all touches known to the system, look at event.allTouches.

If you want all touches known to the system that belong to a specific view, look at [event touchesForView:someView].

UIEvent Class Reference

UPDATE For Swift 3 :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    super.touchesBegan(touches, with: event)
    var countTouch = event?.allTouches?.count

//Do whatever you want with that
}

If you want to know if it changed at any moment, do the same in touchesMoved and put it in an array, you'll be able to analyze it.

Like this :

var countTouch:[Int] = []

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    super.touchesBegan(touches, with: event)
    countTouch.append((event?.allTouches?.count)!) //Not really necessary

//Do whatever you want with that
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    countTouch.append((event?.allTouches?.count)!)
}

Hope it helped someone, don't hesitate to edit or improve it. I'm a beginner in Swift, so it's totally possible there are mistakes.

The issue with your 2nd solution is that in your touchesEnded function, you are setting totalTouch to 0 instead of decrementing by the size of allObjects. The function touchesEnded does not get called when all touches are ended; it is called when any touches are ended.

If I touch once, touchesBegan is called. If I touch again simultaneously, touchesBegan is called again and totalTouch is now at 2. If I lift one of the touches, touchesEnded is called with an allObjects length of 1, because only one touch was lifted. If we had set the counter to 0 here instead of decrementing the counter by the size of allObjects, this would mess up the whole process. This is my solution:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    ...
    touchCount += touches.allObjects.count
    ...
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    ...
    touchCount -= touches.allObjects.count
    ...
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top