2015-10-17 61 views
1

我有一個長時間按下後翻轉的UIView。在模擬器中精美地工作,但在現實世界中,人的手指在按壓時有微小的移動。這些微小的動作重置手勢並立即觸發手勢結束狀態。iOS8 - 如何更改iOS中長按手勢的靈敏度8

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

您需要在手勢識別器的處理程序中正確檢查手勢的狀態。

嘗試:

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

UILongPressGestureRecognizer有一個allowableMovement財產。這是你正在尋找的。它可以讓用戶通過屬性確定的像素距離移動手指,而不會導致手勢結束。默認值是10分。在初始化時將其設置爲大於10的值。