1

我有一个 UIView 长按时会翻转。在模拟器中效果很好,但在现实世界中,人的手指在按下时会有微小的动作。这些微小的动作重置手势并立即触发手势结束状态。

- (void)viewDidLoad {
...

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];
    longPress.minimumPressDuration = 0.7;
    [self.view addGestureRecognizer:longPress];
}


- (void)didLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    if ( gestureRecognizer.state == UIGestureRecognizerStateBegan )
    {
        [UIView transitionFromView:self.questionCardView toView:self.answerCardView
                          duration:1.0
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        completion:nil];
    }
    else
    {
        [UIView transitionFromView:self.answerCardView toView:self.questionCardView
                          duration:1.0
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        completion:^(BOOL finished){
                            [self.view addSubview:self.questionCardView];
                            [self.view sendSubviewToBack:self.questionCardView];
                        }];
    }
}
4

2 回答 2

4

您需要在手势识别器的处理程序中正确检查手势的状态。

尝试:

- (void)didLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    if ( gestureRecognizer.state == UIGestureRecognizerStateBegan )
    {
        [UIView transitionFromView:self.questionCardView toView:self.answerCardView
                          duration:1.0
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        completion:nil];
    }
    else if ( gestureRecognizer.state == UIGestureRecognizerStateEnded )
    {
        [UIView transitionFromView:self.answerCardView toView:self.questionCardView
                          duration:1.0
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        completion:^(BOOL finished){
                            [self.view addSubview:self.questionCardView];
                            [self.view sendSubviewToBack:self.questionCardView];
                        }];
    }
}

正如您所拥有的那样,else除了手势结束之外,每个小动作都会调用该块。

于 2015-10-17T22:45:26.027 回答
1

UILongPressGestureRecognizer有财产allowableMovement。这就是你要找的。它允许用户将手指移动由属性确定的像素距离,而不会导致手势结束。默认值为 10 点。在初始化时将其设置为大于 10 的值。

于 2015-10-18T09:39:15.853 回答