开发者

Weird issue with touchesMoved with multiple touches

I have code in place which lets you tap buttons and play a sound for each button, as well as sliding your fingers across the buttons and having each button play it's corresponding sound as you enter the button's area. It's working pretty good, except for a strange bug:

If you press your fingers on two buttons at the same time it will play both sounds initially. However, if you continue leaving your fingers on the button, and then release your fingers off the screen while moving one of them ever so slightly (still within the confines of each button), it will play one of the sounds of the two buttons. How can I prevent this from happening? I only want the sounds to play if you're sliding a finger across the buttons or hitting them initially, not if you're just moving them slightly while hitting two buttons at the same time and then releasing your finger.

Below is my code:

-(v开发者_运维问答oid)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    NSSet *allTouches = [event allTouches];
    touchesCount = [allTouches count];

    for(UITouch *t in touches) {

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

        if(CGRectContainsPoint(soundButton1.frame, location)) 
        {
            if (!soundButton1.isHighlighted && touchesCount < 2){
                soundID = @"0";
                [self playSound];
                [soundButton1 setHighlighted:YES];
            }
        }
        else {
            [soundButton1 setHighlighted:NO];
        }
        if(CGRectContainsPoint(soundButton2.frame, location)) 
        {
            if (!soundButton2.isHighlighted && touchesCount < 2){
                soundID = @"1";
                [self playSound];
                [soundButton2 setHighlighted:YES];
            }
        }
        else {
            [soundButton2 setHighlighted:NO];
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    for(UITouch *t in touches) {

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

          if(CGRectContainsPoint(soundButton1.frame, location)) 
        {
            [soundButton1 setHighlighted:YES];
            soundID = @"0";
            [self playSound];
        }
        if(CGRectContainsPoint(soundButton2.frame, location)) 
        {
            [soundButton2 setHighlighted:YES];
            soundID = @"1";
            [self playSound];
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  for(UITouch *t in touches) {
    CGPoint location = [t locationInView:t.view];
         if(CGRectContainsPoint(soundButton1.frame, location)) 
            {
            [soundButton1 setHighlighted:NO];
        }
        if(CGRectContainsPoint(soundButton2.frame, location)) 
        {
            [soundButton2 setHighlighted:NO];
        }
   }
}


Try disabling the sounds for a short (very) period. In touchesEnded:withEvent:, check whether it has two touches, then do something like this,

soundDisabled = YES;

You can either request a selector to run after delay

[self performSelector:@selector(enableSound) withObject:nil afterDelay:0.2];

and then

- (void)enableSound {
    soundDisabled = NO;
}

Or you could wait for the second finger to go up and then

soundDisabled = NO;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜