UIAlertView警报在长按手势识别器中重复三次

我创build了一个应用程序 通过开发它,我用长button显示警报。

这是我的代码:

- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { // label1.text = @"Select Iran to observe its historical data projections "; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Press the blue button (+) to select your region " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } 

问题:当我想通过使用cancelIbutton取消UIAlertView,我必须按下这个button三次取消UIAlertView。 这意味着UIAlterview不能被一个媒体取消。 你可以帮我吗?

问题是你正在显示多个警报视图。 你的长按处理程序将被调用不同的状态。

UILongPressGestureRecognizer的文档:

长按手势是连续的。 当在指定的时间段(minimumPressDuration)按下允许的手指的数量(numberOfTouchesRequired)并且触摸不超过允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。 当手指移动时,手势识别器转换到改变状态,并且当任何手指抬起时手势识别器结束(UIGestureRecognizerStateEnded)。

所以你最终会显示“开始”状态的警报,然后显示“已更改”状态,并再次显示“已结束”状态。 如果你移动你的手指,你会得到一个“改变”状态的stream,你最终也会显示每个人的警报。

您需要决定何时实际需要警报出现。 你想让它出现在第一个“改变”的状态,或者当用户举起他们的手指,你得到了“结束”的状态。

你的代码需要是这样的:

 - (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { // label1.text = @"Select Iran to observe its historical data projections "; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Press the blue button (+) to select your region " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } } 

这会在用户抬起手指时显示警报。 如果要在识别长UIGestureRecognizerStateChanged立即显示警报,请将UIGestureRecognizerStateEnded更改为UIGestureRecognizerStateChanged 。 但请记住,由于长时间按压可能会产生多个“更改”状态,因此您仍然可以获得倍数。 在这种情况下,你需要添加一个额外的检查,只显示警报,如果还没有。

实际上,这里有一个简单的方法来支持“更改”状态下的单个警报:

 - (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateChanged) { sender.enabled = NO; // Prevent any more state updates so you only get this one sender.enabled = YES; // reenable the gesture recognizer for the next long press // label1.text = @"Select Iran to observe its historical data projections "; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Press the blue button (+) to select your region " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } } 

尝试这个

 - (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { // label1.text = @"Select Iran to observe its historical data projections "; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Press the blue button (+) to select your region " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; alert.tag =10; [alert show]; } - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { switch (alertView.tag) { case 10: if (buttonIndex==0) { } break; } } 

这是一个古老的问题,但我有一个类似的问题。

任何UILongPressGestureRecognizer将至less生成4个状态变化:

  • 改变 – (即改为开始)
  • 开始 – (即开始)
  • 改变 – (即改为结束)
  • 结束 – (即结束)

所以为了处理好,你需要select哪些条件会激活一个动作。

最简单的,对应一个UIButton的“触及内部”就是检测“结束”状态。

下面的例子是'longPress'是UILongPressGestureRecognizer的实例

 - (void)longPressedLastImage:(UILongPressGestureRecognizer*) longPress { if (longPress.state == UIGestureRecognizerStateEnded) { // do what you would in response to an equivalent button press } } 

这可以让你更像UIButton的基本操作。

“简单胜于复杂” – T彼得斯 – Python的禅宗