我有代码,您可以轻按按钮并为每个按钮播放声音,以及在按钮上滑动手指,让每个按钮在您进入按钮区域时播放相应的声音。它的工作非常好,除了一个奇怪的错误:
如果您同时按下两个按钮,它将最初播放两种声音。但是,如果您继续将手指放在按钮上,然后将手指从屏幕上松开,同时稍微移动其中一个(仍然在每个按钮的范围内),它将播放两个按钮的一个声音。我怎样才能防止这种情况发生?我只想让你的手指在按钮上滑动或者最初击中它们时发出声音,而不是只是在你同时按下两个按钮然后放开你的手指时稍微移动它们。
以下是我的代码:
-(void)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];
}
}
}
答案 0 :(得分:0)
尝试短暂(非常)禁用声音。在touchesEnded:withEvent:
中,检查它是否有两次触摸,然后执行类似的操作,
soundDisabled = YES;
您可以请求选择器在延迟后运行
[self performSelector:@selector(enableSound) withObject:nil afterDelay:0.2];
然后
- (void)enableSound {
soundDisabled = NO;
}
或者你可以等待第二根手指上升然后
soundDisabled = NO;