如何處理當前正在處理的手勢等簡單解決方案,並在所有手勢都結束時執行操作?
.h文件中:
int handledGesturesCount;
.m文件:
- (id)init {
(...)
handledGesturesCount = 0;
}
// gesture handlers - the code for -pinch: repeats for -pan: and -rotate:
- (void)pinch:(UIPinchGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
handledGesturesCount += 1;
} else if (recognizer.state == UIGestureRecognizerStateEnded ||
recognizer.state == UIGestureRecognizerStateCancelled ||
recognizer.state == UIGestureRecognizerStateFailed)
{
handledGesturesCount -= 1;
if (handledGesturesCount == 0) {
[self resetImage];
}
}
}
- (void)pan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
handledGesturesCount += 1;
} else if (recognizer.state == UIGestureRecognizerStateEnded ||
recognizer.state == UIGestureRecognizerStateCancelled ||
recognizer.state == UIGestureRecognizerStateFailed)
{
handledGesturesCount -= 1;
if (handledGesturesCount == 0) {
[self resetImage];
}
}
}
- (void)rotate:(UIRotationGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
handledGesturesCount += 1;
} else if (recognizer.state == UIGestureRecognizerStateEnded ||
recognizer.state == UIGestureRecognizerStateCancelled ||
recognizer.state == UIGestureRecognizerStateFailed)
{
handledGesturesCount -= 1;
if (handledGesturesCount == 0) {
[self resetImage];
}
}
}
非常感謝你,它的工作原理。 –
當您使用抽頭識別器時,此方法將失敗。他們似乎在'UIGestureRecognizerStateBegan'時發送消息,導致'handledGesturesCount'減少到0以下。 –