简体   繁体   English

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

[英]iOS8 - How do I change the sensitivity of a long press gesture in iOS 8

I have a UIView that flips over when long pressed. 我有一个UIView,长按时会翻转。 Works beautifully in the simulator, but in the real world the human finger has tiny movements while pressing. 在模拟器中可以很好地工作,但是在现实世界中,人的手指在按压时会有微小的移动。 These tiny movements reset the gesture and instantly trigger the gesture ended state. 这些微小的动作会重置手势并立即触发手势结束状态。

- (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];
                        }];
    }
}

You need to properly check the gesture's state in your gesture recognizer's handler. 您需要在手势识别器的处理程序中正确检查手势的状态。

Try: 尝试:

- (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];
                        }];
    }
}

As you had it, the else block was being called on every little movement in addition to the gesture ending. 如您所愿,除了手势结束外, else块在每个小动作上都被调用。

UILongPressGestureRecognizer has an allowableMovement property. UILongPressGestureRecognizer具有allowableMovement属性。 This is what you are looking for. 这就是您要寻找的。 It lets the user move their finger by the pixel distance determined by the property without causing the gesture to end. 它使用户可以将手指移动属性确定的像素距离,而不会导致手势结束。 The default value is 10 points. 默认值为10点。 Set it to something larger than 10 in your initialisation. 在初始化时将其设置为大于10的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM