ios开发——长按按出两个UIAlertView的解决办法
有时我们希望通过长按手势来删除一些数据,这是一个比较好的用户体验是在删除之前弹出一个UIAlertView来提醒用户进行二次确认。然而,这样会出现一个bug:你定义的UIAlertView会弹出两次,如下图
代码如下:
添加手势部分:
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。