0

My question is when i hold down a button it has a sound at this time i touch on the same button of my another finger, it has a sound also. So can i disable another touch when i have already hold down a finger on the same button? As TouchesMoved as same issue.

int touchesCount;

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

UITouch *touch = [touches anyObject];

CGPoint touchLocation = [touch locationInView:self.view];

if(CGRectContainsPoint(img1.frame,touchLocation)){

    if (!img1.isHighlighted && touchesCount < 1){

        [img1 setHighlighted:YES];
        [img2 setHighlighted:NO];


        NSLog(@" Image 1");

        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"c", CFSTR ("mp3"), NULL);

        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }
}else {
    [img1 setHighlighted:NO];

}if (CGRectContainsPoint(img2.frame,touchLocation)){

    if (!img2.isHighlighted && touchesCount < 1){

        [img2 setHighlighted:YES];
        [img1 setHighlighted:NO];

        NSLog(@" Image 2");

        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"d", CFSTR ("mp3"), NULL);

        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }
}else {
    [img2 setHighlighted:NO];
}

}

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

UITouch *touch = [touches anyObject];

CGPoint touchLocation = [touch locationInView:self.view];

if(CGRectContainsPoint(img1.frame,touchLocation)){

    if (!img1.isHighlighted && touchesCount < 1){

        [img1 setHighlighted:YES];
        [img2 setHighlighted:NO];

        NSLog(@" Image 1");

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"c", CFSTR ("mp3"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
    }
}else {
    [img1 setHighlighted:NO];

}if (CGRectContainsPoint(img2.frame,touchLocation)){

     if (!img2.isHighlighted && touchesCount < 1){

         [img2 setHighlighted:YES];
         [img1 setHighlighted:NO];

    NSLog(@" Image 2");

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"d", CFSTR ("mp3"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
     }
}else {
    [img2 setHighlighted:NO];
}

}

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

[img1 setHighlighted:NO];
[img2 setHighlighted:NO];

}

  • (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self touchesEnded:touches withEvent:event]; }

1 Answers1

1

Since you are detecting touches of your "buttons" using tapped point coordinates, you should simply add logic in your touchesBegan to check if your button is already tapped. E.g. add a "BOOL button1Tapped" variable to class.

Daniel
  • 1,101
  • 9
  • 18