How do you get multiple touch buttons to work with

2019-05-25 06:50发布

问题:

I have a bunch of buttons that I want to activate in three different ways.

  1. Touch Down
  2. Touch Down - multiple touch (at the same time)
  3. Touch Drag Inside (The same as dragging your finger over a piano)

The first two is obviously easy in IB. However many people, including myself, have had trouble with Touch Drag inside. So I ended up using - (void) touchesMoved [see code] . This works great for the drag... but to get it to work I had to disable the buttons "user Interaction" in IB. Which means I lost the "Touch Down" and multi-touch capabilities.

So, in order to get the "Touch Down" to work, I used -(void) touchesBegan [see code]. This works fine, but I can't get multi-touch to work.

Does anyone know how I can get my buttons to fire simultaneously during multi-touch?
Or ... Is there a way to get touches moved and button functions in IB to work together?

I have tried touch.view.multiTouchEnabled = Yes; and I have ensured that my buttons are multiple touch ok in IB... But nothing.

Below is my code. Thank you very much for your help.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self.view] anyObject];

    CGPoint location = [touch locationInView:touch.view];

    if(CGRectContainsPoint(p1.frame, location)) 
    {
        if (!p1.isHighlighted){
            [self pP01];
            [p1 setHighlighted:YES];
    }
}else {
        [p1 setHighlighted:NO];
    }
    //
    if(CGRectContainsPoint(p2.frame, location)) 
    {
        if (!p2.isHighlighted){
            [self pP02];
            [p2 setHighlighted:YES];
        }
    }else {
        [p2 setHighlighted:NO];
    }
    if(CGRectContainsPoint(p3.frame, location))
    {
        if (!p3.isHighlighted){
            [self pP03];
            [p3 setHighlighted:YES];
        }
    }else {
        [p3 setHighlighted:NO];
    }
}

///

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [[event touchesForView:self.view] anyObject];
        CGPoint location = [touch locationInView:touch.view];

        if(CGRectContainsPoint(p1.frame, location))
        {
            [self pP01];
            [p1 setHighlighted:YES];
        }
        if(CGRectContainsPoint(p2.frame, location))
        {
            [self pP02];
            [p2 setHighlighted:YES];
        }
        if(CGRectContainsPoint(p3.frame, location))
        {
            [self pP03];
            [p3 setHighlighted:YES];
        }
}

回答1:

You need to check every touch instead of one random touch. So, for(UITouch *t in touches) instead of UITouch *touch = [touches anyObject]