iOS8 - 如何在iOS 8中更改长按手势的灵敏度

时间:2015-10-17 22:34:29

标签: ios objective-c cocoa-touch ios8 uilongpressgesturerecogni

我有一个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];
                        }];
    }
}

2 个答案:

答案 0 :(得分: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块。

答案 1 :(得分:1)

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