1

是否可以只触发一次 UIPinchGestureRecognizer 动作?

我想让我的用户能够捏(实际上是传播)以触发一个动作,但是该方法会不断被调用,我理解这是 UIPinchGestureRecognizer 的功能。

使用以下答案更新代码(仅检测缩放):

-(void) handlePinchGesture: (UIPinchGestureRecognizer *) sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        self.startingScale = sender.scale;
    }
    if (sender.state == UIGestureRecognizerStateEnded) {
        if (sender.scale > self.startingScale) {
            [self zoomIn];
        }
    }
}
4

1 回答 1

6

手势识别器是状态机,如果您没有指定要收听的状态,只需从手势中调用选择器即可触发所有/每个状态。

要将检测限制在手势开始时,您可以使用:

- (void)myGestureRecognized:(UIGestureRecognizer*)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        //do something
    }
}

其他识别状态包括:

typedef enum {
   UIGestureRecognizerStatePossible,

   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,

   UIGestureRecognizerStateFailed,

   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;
于 2012-09-11T20:05:57.770 回答