touchesBegan&touchesMoved

And*_*wDK 6 iphone xcode cocoa-touch objective-c

所以这是一个奇怪的问题.我有一个响应touchesMoved:&的UIImageView touchesBegan:,它完美地运行.我遇到的主要问题是如果你把手指放在说image1然后然后用手指按住图像1你会用另一根手指按下image2.

反过来,这将再次触发image1.不希望这种情况发生.我希望能够在同时推动两个图像时使用Multi Touch,而不是一遍又一遍地触发它们.必须有一些东西我必须添加,以阻止这种情况发生.

码:

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

    }
Run Code Online (Sandbox Code Playgroud)

并为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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

最后这里是决赛:

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

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

所以我只需要这样做,如果我按住其中一个图像,然后再按下另一个图像并再次释放它再次推动它停止发射我按下的第一个图像.

Jas*_*man 0

使用传递到方法中的 NSSet 触摸touchesBegan:,而不是allTouches:从事件中传递。