【问题标题】:touchesBegan & touchesMovedtouchesBegan & touchesMoved
【发布时间】:2011-03-21 05:16:22
【问题描述】:

所以这是一个奇怪的问题。我有一个响应 touchesMoved:touchesBegan: 的 UIImageView,它运行良好。我遇到的主要问题是,如果您将手指放在说 image1 上,然后该手指仍然在 image1 上,您将用另一根手指按 image2。

这又会再次触发 image1。不希望发生这种情况。我希望能够在同时推动两个图像而不是一遍又一遍地触发它们时使用多点触控。我必须添加一些东西来阻止这种情况的发生。

代码:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches]; 
    for (UITouch *touch in allTouches) 
    { 
        CGPoint location = [touch locationInView:touch.view];
        if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image1;
        }
        if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image2;
        }

    }

对于touchesMoved:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
        if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image1;
        }
        if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image2;
        }

    }

最后是决赛:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    lastButton = nil;
}

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

所以我只需要做到这一点,如果我按住其中一个图像,然后按下另一个图像并再次释放它并再次按下它,它就会停止触发我按下的第一张图像。

【问题讨论】:

    标签: iphone objective-c cocoa-touch xcode


    【解决方案1】:

    使用传递给touchesBegan: 方法的touches NSSet,而不是来自事件的allTouches:

    【讨论】:

    • 你会怎么写出来?我尝试使用:-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event touchesForView:self.view] anyObject]; CGPoint location = [touch locationInView:touch.view];我不认为那是你的意思。介意把它写出来,这样我就明白了。谢谢。
    • 该方法的参数之一是 NSSet*,它是受影响的触摸。所以在 -(void)touchesBegan:(NSSet )touches withEvent:(UIEvent *)event {...} 中只需使用 *that touches 变量。该集合将包含在该事件期间开始的触摸。 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; // 无论你想用触摸做什么... }
    • 是的,感谢您的澄清,它仍然无法正常工作。当我将手指放在一个图像上然后按下另一个图像时,它仍然会触发它。同样,当我尝试一次推送 2 张图像时,它们会触发,然后第一个击中的图像会随着触摸再次触发。真的很奇怪。
    【解决方案2】:

    所有涉及的视图都将收到所有的触摸(在 Jason 提到的 NSSet 中)。取决于每个视图(即取决于您)选择您感兴趣的触摸。因此,您必须检查每个触摸是否在视图的限制范围内,并尝试将触摸与收到的最后一个触摸相关联。例如,由于您在一组中进行了接触,因此您不能依靠它们的顺序来计算它们移动了多少。您必须保存最后一次触摸的坐标并选择最近的触摸并假设它是移动的同一根手指。

    【讨论】:

    • 有点混乱,我明白你的意思。我得再玩一会儿。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多