Also hier ist eine seltsame Frage. Ich habe eine UIImageView, die auf touchesMoved:
& touchesBegan:
reagiert, es funktioniert perfekt. Das Hauptproblem, das ich habe, ist, wenn du deinen Finger auf Bild 1 legst und dann mit demselben Finger immer noch auf Bild 1 drückst und dann mit deinem anderen Finger auf Bild 2 drückst.
In diesem Fall würde Bild 1 erneut ausgelöst werden. Das möchte ich nicht. Ich möchte Multi-Touch verwenden können, wenn ich beide Bilder gleichzeitig drücke, ohne sie immer wieder auszulösen. Es muss etwas geben, das ich hinzufügen muss, um das zu verhindern.
Code:
-(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) {
//Sound abspielen
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) {
//Sound abspielen
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2"
ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
//
lastButton = image2;
}
}
Und für die 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) {
//Sound abspielen
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) {
//Sound abspielen
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2"
ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
//
lastButton = image2;
}
}
Zuletzt hier sind die Finals:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
lastButton = nil;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
Also muss ich es nur so machen, dass wenn ich eines der Bilder gedrückt halte und dann ein anderes drücke und wieder loslasse und erneut drücke, es aufhört, das erste Bild auszulösen, das ich gedrückt halte.