多次触摸时 TouchMoved 的奇怪问题

发布于 2024-11-09 00:27:02 字数 2236 浏览 0 评论 0原文

我已经编写了代码,可以让您点击按钮并为每个按钮播放声音,以及在按钮上滑动手指并让每个按钮在您进入按钮区域时播放相应的声音。它工作得很好,除了一个奇怪的错误:

如果你同时按两个按钮,它首先会播放两种声音。但是,如果您继续将手指放在按钮上,然后将手指从屏幕上松开,同时稍微移动其中一根手指(仍在每个按钮的范围内),它将播放两个按钮的声音之一。我怎样才能防止这种情况发生?我只希望当您在按钮上滑动手指或最初点击它们时播放声音,而不是当您在同时点击两个按钮时稍微移动它们然后释放手指时播放声音。

下面是我的代码:

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2024-11-16 00:27:02

尝试在很短(非常)的时间内禁用声音。在 touchesEnded:withEvent: 中,检查是否有两次触摸,然后执行类似的操作,

soundDisabled = YES;

您可以请求选择器在延迟后运行

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

,然后

- (void)enableSound {
    soundDisabled = NO;
}

或者您可以等待第二根手指向上,然后然后

soundDisabled = 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文