0

我的问题是当我按住一个按钮时它有声音,此时我触摸我另一根手指的同一个按钮,它也有声音。那么当我已经在同一个按钮上按住手指时,我可以禁用另一个触摸吗?与 TouchesMoved 相同的问题。

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]; }
4

1 回答 1

1

由于您正在使用点击点坐标检测“按钮”的触摸,因此您只需在 touchesBegan 中添加逻辑以检查您的按钮是否已被点击。例如,在类中添加一个“BOOL button1Tapped”变量。

于 2013-01-16T00:50:49.457 回答