UIAlertView警报在长按手势识别器中重复三次 [英] UIAlertView alert repeat three times within long press gesture recognizer

查看:80
本文介绍了UIAlertView警报在长按手势识别器中重复三次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序。通过开发它,我使用长按按钮显示警告。

I created an application. Through developing it, i used long press button to show an alert.

这是我的代码:

- (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时,我必须按此按钮三次取消UIAlertView。这意味着UIAlterview只能通过一个印刷机取消。
你能帮我吗?

Question: When I want to cancel the UIAlertView by using the cancelIbutton, I must press this button three times to cancel the UIAlertView. it means UIAlterview cannot be canceled just by one press. Could you please help me?

推荐答案

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

The problem is that your are showing more than one alert view. Your long press handler will get called for different states.

来自 UILongPressGestureRecognizer的文档


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

Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.

因此,您最终会显示开始状态的警报,然后显示已更改状态,并再次显示结束状态。如果你移动手指就会得到一个已更改状态的流,你最终也会显示每个状态的警报。

So you end up displaying an alert for the "Began" state, then the "Changed" state, and again for the "Ended" state. If you moved your finger around you would get a stream of "Changed" states and you would end up showing an alert for every one of them too.

你需要决定当你真的想要出现警报时。您是否希望它出现在第一个已更改状态,或者当用户抬起手指并且您获得结束状态时。

You need to decide when you actually want the alert to appear. Do you want it to appear at the first "Changed" state or when the user lifts their finger and you get the "Ended" state.

您的代码需要是某种东西像这样:

Your code needs to be something like this:

- (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];
    }
}

这将显示用户抬起手指时的提示。如果您希望在识别出长按后立即显示警报,请将 UIGestureRecognizerStateEnded 更改为 UIGestureRecognizerStateChanged 。但请记住,您可能仍会获得倍数,因为长按会产生多个已更改状态。在这种情况下,您需要添加一个额外的检查以仅显示警报(如果还没有)。

This will show the alert when the user lifts their finger. Change UIGestureRecognizerStateEnded to UIGestureRecognizerStateChanged if you want the alert to appear as soon as the long press is recognized. But keep in mind you may still get multiples since a long press can generate multiple "Changed" states. In this case you need to add an additional check to only show the alert if there isn't already one.

实际上,这是一种支持单个警报的简单方法已更改状态:

Actually, here's an easy way to support a single alert on the "Changed" state:

- (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];
    }
}

这篇关于UIAlertView警报在长按手势识别器中重复三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆