简体   繁体   中英

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. 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.

UILongPressGestureRecognizer has an allowableMovement property. 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. Set it to something larger than 10 in your initialisation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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