ios开发——长按按出两个UIAlertView的解决办法

有时我们希望通过长按手势来删除一些数据,这是一个比较好的用户体验是在删除之前弹出一个UIAlertView来提醒用户进行二次确认。然而,这样会出现一个bug:你定义的UIAlertView会弹出两次,如下图

ios开发——长按按出两个UIAlertView的解决办法_第1张图片

代码如下:

添加手势部分:

  1. self.longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];  
  2. [self addGestureRecognizer:self.longPressGR];  
    self.longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [self addGestureRecognizer:self.longPressGR];

手势方法:
  1. - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {  
  2.     if (self.delegate == nil) {  
  3.         return;  
  4.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要删除该课程吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  
  5.     [alert show];  
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {

    if (self.delegate == nil) {
        return;
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要删除该课程吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];

}

UIAlertView的delegate
  1. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
  2.     if (buttonIndex == 1) {  
  3.         [self deleteData];  
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [self deleteData];
    }
}

这样做的原因是,我们都知道,手势都有state属性,每个手势有begin,recognized,ended等状态,而UILongPressGestureRecognizer是一个“长过程”手势,其具体的状态变化和触发方法时机是难料的,因此一个有效而简单地办法是在触发方法中添加判断,只有当begin状态才触发这个方法,这样就能解决了
  1. - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {  
  2.     if (recognizer.state == UIGestureRecognizerStateBegan) {  
  3.         if (self.delegate == nil) {  
  4.             return;  
  5.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要删除该课程吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  
  6.         [alert show];  
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        if (self.delegate == nil) {
            return;
        }
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要删除该课程吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

效果图:
ios开发——长按按出两个UIAlertView的解决办法_第2张图片

附上手势的状态以及这些状态中可以触发方法的状态和时机。来自Apple的UIGestureRecognizer.h文件中.

  1. UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state  
  2. UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop  
  3. UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop  
  4. UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible  
  5. UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible  
  6. UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible  
  7. // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled  
  8. UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible  
    UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
    
    UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
    
    UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
    
    // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(ios开发——长按按出两个UIAlertView的解决办法)