我有一个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];
}];
}
}
答案 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的值。