Итак, вот странный вопрос. У меня есть UIImageView, который отвечает на touchesMoved:
& touchesBegan:
, он работает отлично. Главное, с чем у меня проблема, - это если вы положите палец, скажем, на image1, а затем, не отпуская палец, на image1, вы нажмете на 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;
}
}
А за прикосновения установлено:
-(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];
}
Так что мне просто нужно сделать так, чтобы я удерживал одно из изображений, а затем нажимал другое, отпускал его снова и снова нажимал, чтобы он прекратил запуск первого изображения, на которое я нажимаю.