UIAlertView alert repeat three times within long press gesture recognizer

烈酒焚心 提交于 2019-12-19 12:08:30

问题


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

This is my code:

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

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?


回答1:


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

From the docs for UILongPressGestureRecognizer:

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

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



回答2:


Try this

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



回答3:


This is an old question, but I had a similar issue.

Any UILongPressGestureRecognizer will generate at least 4 changes of state:

  • changed - (i.e. changed to started)
  • began - (i.e. started)
  • changed - (i.e. changed to ended)
  • ended - (i.e. ended)

So to handle it well, you need to choose which of those conditions will activate an action.

The simplest, which sort of corresponds to 'touch up inside' for a UIButton is to detect the 'ended' state.

An example is below where 'longPress' is the instance of UILongPressGestureRecognizer

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

This lets you treat the continuous press more like the basic UIButton action.

'simple is better than complex' - T Peters - the Zen of Python



来源:https://stackoverflow.com/questions/15306942/uialertview-alert-repeat-three-times-within-long-press-gesture-recognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!