How do you get multiple touch buttons to work with touchesBegan/Moved?

烂漫一生 提交于 2019-12-07 22:54:39

问题


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]



来源:https://stackoverflow.com/questions/2286305/how-do-you-get-multiple-touch-buttons-to-work-with-touchesbegan-moved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!