ios开发——长按按出两个UIAlertView的解决办法
2014-11-27 10:34
447 查看
有时我们希望通过长按手势来删除一些数据,这是一个比较好的用户体验是在删除之前弹出一个UIAlertView来提醒用户进行二次确认。然而,这样会出现一个bug:你定义的UIAlertView会弹出两次,如下图
代码如下:
添加手势部分:
手势方法:
UIAlertView的delegate
这样做的原因是,我们都知道,手势都有state属性,每个手势有begin,recognized,ended等状态,而UILongPressGestureRecognizer是一个“长过程”手势,其具体的状态变化和触发方法时机是难料的,因此一个有效而简单地办法是在触发方法中添加判断,只有当begin状态才触发这个方法,这样就能解决了
效果图:
附上手势的状态以及这些状态中可以触发方法的状态和时机。来自Apple的UIGestureRecognizer.h文件中.
代码如下:
添加手势部分:
self.longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; [self addGestureRecognizer:self.longPressGR];
手势方法:
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { if (self.delegate == nil) { return; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定要删除该课程吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alert show]; }
UIAlertView的delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { [self deleteData]; } }
这样做的原因是,我们都知道,手势都有state属性,每个手势有begin,recognized,ended等状态,而UILongPressGestureRecognizer是一个“长过程”手势,其具体的状态变化和触发方法时机是难料的,因此一个有效而简单地办法是在触发方法中添加判断,只有当begin状态才触发这个方法,这样就能解决了
- (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]; } }
效果图:
附上手势的状态以及这些状态中可以触发方法的状态和时机。来自Apple的UIGestureRecognizer.h文件中.
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的解决办法
- iOS开发-UITableViewCell(UICollectionViewItem)需要两个手指点击或者长按才可以选中,解决办法
- [绍棠]iOS开发-UITableViewCell(UICollectionViewItem)需要两个手指点击或者长按才可以选中,解决办法
- iOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
- IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
- iOS开发- iOS7显示偏差(UITableView下移)解决办法
- iOS开发 关于tableView加载图片时出现卡顿时的解决办法
- IOS开发问题解决之UIAlertView调用UIActionSheet后灰色背景一直存在
- iOS开发:MKMapView地图内存持续增加的释放解决办法
- iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 的解决办法
- IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
- iOS开发:MKMapView地图内存持续增加的释放解决办法
- iOS开发 iOS7显示偏差(UITableView下移)解决办法
- iOS开发中Base SDK Missing的两个解决办法
- IOS开发问题解决之UIAlertView调用UIActionSheet后灰色背景一直存在
- [IOS开发记录]Whose view is not in the window hierarchy 错误的解决办法
- iOS软件开发之在UIScrollView上添加手势不能触发解决办法
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!更新解决添加组件Cocos2d动画停止播放的BUG】
- ios 4.3 中 viewDidAppear没有被调用的解决办法
- 【无限互联】iOS开发block用法之UIAlertView控件的修改