ios开发——长按按出两个UIAlertView的解决办法
2015-09-23 14:22
211 查看
有时我们希望通过长按手势来删除一些数据,这是一个比较好的用户体验是在删除之前弹出一个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 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 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
版权声明:本文为博主原创文章,未经博主允许不得转载。
代码如下:
添加手势部分:
self.longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self addGestureRecognizer:self.longPressGR];
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 nil];
[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
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[self deleteData];
- (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 nil];
[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]; } }
效果图:
附上手势的状态以及这些状态中可以触发方法的状态和时机。来自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
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
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- Axure-产品交互设计师的利器
- 将原生的Launcher修改成类似MiUI的Launcher
- 算法练习:Sequence II
- Great StackOverflow questions
- Android 高级UI
- iOS8下,如何将UIButton的字体颜色设置成系统自带的字体颜色?
- iOS UI基础-14.0 DatePicker
- Xcode7 You must rebuild it with bitcode enabled (Xcodesetting ENABLE_BITCODE)
- Flex Builder启动项目一直停留在57%
- IOS键盘的相关设置(UITextfield)
- UITextField的总结
- A1101. Quick Sort (25)
- does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE
- UEStudio 14.30.1005 简体中文增强版(程序员必备代码编辑器)
- ndk-build的用法
- Java for LeetCode 232 Implement Queue using Stacks
- 怎么控制UIStoryboardSegue是否进行跳转?
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- IOS中UIScrollView的contentSize、contentOffset和contentInset属性
- *Unique Binary Search Tree