[英]iOS8 - How do I change the sensitivity of a long press gesture in iOS 8
我有一個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];
}];
}
}
您需要在手勢識別器的處理程序中正確檢查手勢的狀態。
嘗試:
- (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
塊在每個小動作上都被調用。
UILongPressGestureRecognizer
具有allowableMovement
屬性。 這就是您要尋找的。 它使用戶可以將手指移動屬性確定的像素距離,而不會導致手勢結束。 默認值為10點。 在初始化時將其設置為大於10的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.