I am trying to play different animations and sounds depending on which button the user presses. The buttons are shaped variously and I need to play a sound when the user is holding the button down and stop them while he lifts up his finger. I thought it would be easy just doing with touchesBegan and touchesMoved.
However, if the user moves his finger while touching the button (even a 1 pixel movement), then there is touchesMoved method called. So, I tried some options and I am able to stop the sound once the finger moves (by calling touchesEnded by myself), however it is not the perfect solution, because the user moves the finger even without him noticing (like 1 pixel or so) and then it is very hard to play the sound continuously while he is touching the button.
So I thought I could create two Integers, which to one I will set value to in touchesBegan, then in touchesMoved setting the another and lastly comparing them, checking if the move is in the same view (button) - if it is not then it calls the touchesEnded. However it has one problem, and that is if the user holds his finger on the button, then moves it (still on the same button) and then he lifts up, the touchesEnded is not called, because he started and moved in the same view.
What should I do to call the touchesEnded method after user lifts up his finger after moving it?
Here is my code (ignore those alpha settings, playing sounds etc.):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == leftArmBtn) {
leftArmBtn.alpha = 0;
leftLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"leftArmPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"LEFTARM"];
touchParent = 1;
} else if ([touch view] == mouthBtn) {
mouthBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"mouthPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"MOUTH"];
touchParent = 2;
} else if ([touch view] == rightArmBtn) {
rightArmBtn.alpha = 0;
righLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"rightArmPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"RIGHTARM"];
touchParent = 3;
} else if ([touch view] == leftLegBtn) {
leftLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"leftLegPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"LEFTLEG"];
touchParent = 4;
} else if ([touch view] == righLegBtn) {
righLegBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"rightLegPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"RIGHTLEG"];
touchParent = 5;
} else if ([touch view] == vakBtn) {
vakBtn.alpha = 0;
mainImageView.image = [UIImage imageNamed:@"vakPush.jpg"];
[[SoundManagerOAL sharedSoundManagerOAL] playSoundWithKey:@"VAK"];
touchParent = 6;
} else {
touchParent = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == leftArmBtn) {
leftLegBtn.alpha = 1;
leftArmBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"LEFTARM"];
} else if ([touch view] == mouthBtn) {
mouthBtn.alpha = 1;
mainImageView.image = defaultImage;
} else if ([touch view] == rightArmBtn) {
rightArmBtn.alpha = 1;
righLegBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"RIGHTARM"];
} else if ([touch view] == leftLegBtn) {
leftLegBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"LEFTLEG"];
} else if ([touch view] == righLegBtn) {
righLegBtn.alpha = 1;
mainImageView.image = defaultImage;
[[SoundManagerOAL sharedSoundManagerOAL] stopSoundWithKey:@"RIGHTLEG"];
} else if ([touch view] == vakBtn) {
vakBtn.alpha = 1;
mainImageView.image = defaultImage;
} else {
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == leftArmBtn) {
movingTouch = 1;
} else if ([touch view] == mouthBtn) {
movingTouch = 2;
} else if ([touch view] == rightArmBtn) {
movingTouch = 3;
} else if ([touch view] == leftLegBtn) {
movingTouch = 4;
} else if ([touch view] == righLegBtn) {
movingTouch = 5;
} else if ([touch view] == vakBtn) {
movingTouch = 6;
} else {
movingTouch = 10;
}
if (touchParent != movingTouch) {
[self touchesEnded:touches withEvent:event];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
touchesEnded:withEvent:
call should fire irregardless where the user holds her finger. By the way, remove that Xcode tag from the question. - user529758