user1195202
user1195202

Reputation: 1443

iPhone/iPad identity which touchBegin, touchEnd

I'm trying to build simple app with multiple touch. I'm newbie with iOS and objectivec/cocoa. So please sorry if question is so stupid.

I want to know how many fingers now active and after user release some finger or all fingers, which was released. I want to pay attention, I need to know exactly which finger was released. On the first look seems all is easy... but on practice I'm getting a problem (

Let's say I put down at the same time 3 fingers on the screen, then after some time I put my fourth finger. In this case in my UIView.m file were called two 'touchesBegan' evens. In first event I got count of touches = 3', in second count of touches will be = 4' (And that is right because this is a total counter). So with this event seems to be everything is clear.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];
    NSArray *allObjects=[allTouches allObjects];
    touchCount = [NSNumber numberWithInt:[allObjects count]]; // this is my main counter of current active touches.
    NSLog(@"%d", touchCount.intValue);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //what I should do ????
}

Let's divide the screen into 4 parts. Imagine a 4 rectangles on the iPad screen.

And let's create four 4 boolean variables for this rectangles. Were we will be store status of all 4 rectangles. By default all variables will be - false;

bool rectangle1 = false;
bool rectangle2 = false;
bool rectangle3 = false;
bool rectangle4 = false;

Then when finger begin down, in the 'touchesBegan' event we changing boolean variables. Which variable we should change I'm know by x-y coordinates. So let's say I put down my finger on rectangle #2, then later I put down another finger on rectangle #4. In this case variables will be following:

bool rectangle1 = false;
bool rectangle2 = true;
bool rectangle3 = false;
bool rectangle4 = true;

After this I'm releasing one finger from rectangle #4. So how I will be know in the 'touchesEnded' event that this is were released finger which was from the start were on the rectangle #4 area?

Upvotes: 0

Views: 1444

Answers (3)

warrenm
warrenm

Reputation: 31782

UITouch has two methods for determining the current and former location of a touch: locationInView: and previousLocationInView:. These methods provide the only reliable way to establish the "identity" of a touch across calls to touchesMoved:withEvent:. By keeping track of the value returned by locationInView: and comparing it in subsequent calls to the value returned by previousLocationInView: you can unambiguously establish "which" touch you're looking at.

You didn't provide enough information to determine whether what you're trying to do can be done with UIGestureRecognizers, but if it can, you'll probably find them much easier to work with.

Upvotes: 0

picciano
picciano

Reputation: 22701

UIGestureRecognizers are definitely the right approach. I would take it a step further and have each rectangle have it's own gesture recognizer, or at the very least, their own implementations of touchesBegan and touchesEnded.

If more coordination is needed between the rectangles, then it should be the view controller's job. The rectangle can notify the view controller as needed.

Upvotes: 0

Oscar Gomez
Oscar Gomez

Reputation: 18488

Instead of writing that custom code, I would strongly suggest using this classes instead: UIGestureRecognizer, there are concrete implementations to achieve what you want.

Upvotes: 1

Related Questions