views:

77

answers:

1

Im having a little problem on handling touches in my apps.

I set my touchesBegan like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *currentTouch = [[event allTouches] anyObject];
    touchPoint = [currentTouch locationInView:self.view];
    if (CGRectContainsPoint(image1.frame, touchPoint)) {
        image1IsTouched = YES;
    }
}

Then i set my touch move like this:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    UITouch *currentTouch = [[event allTouches] anyObject];
    currentPoint = [currentTouch locationInView:currentTouch.view];

    if(image1IsTouched == YES) {
        image1.center = CGPointMake(currentPoint.x,currentPoint.y);
        .....
    }
}

Now i tried my app on actual unit and thats where i notice my problem. While im touching the image1 with 1 finger the app is doing ok and its checking for collision everytime i drag my finger. The problem occurs when i touch the screen with another finger while touching/dragging the image. The image im currently touching will jump to the other finger. I've tried [myView setMultipleTouchEnable:NO]; & using NSArray on touches and comparing the [touches count] with the touch but its not working. Can someone show me how to set a uiimageview to act on single touch only. Thanks.

A: 

First, you should use UITouch *currentTouch = [touches anyObject]; to get the current touch.

Second, you should check that touches.count == 1 to make sure there's only one finger on the screen, and ignore touch input if there's more than one, unless you wanted to support multitouch.

lucius
you mean i should use "UITouch *currentTouch = [touches anyObject];" instead of "UITouch *currentTouch = [[event allTouches] anyObject];" whats the difference between the two anyway? Sorry for some noob question :).
Drahc
Wow i've just tested it and it works perfectly. Thanks a lot. :)
Drahc
[event allTouches] gets all the fingers on your screen, even the ones that are outside to your view. It's useful for when you want to make sure you get all the touches, even stray ones.
lucius
yeah i got it after watching the iphone programming video on itunes. anyways thanks for the help, really appreciate it.
Drahc