0

私の質問は、ボタンを押したままにすると音が鳴るということです。このとき、別の指で同じボタンをタッチすると、音も鳴ります。同じボタンを指で押したままにしている場合、別のタッチを無効にすることはできますか? 同じ問題として TouchesMoved として。

int touchesCount;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)イベント {

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 *)イベント {

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 *)イベント{

[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 に答える