长按手势识别器触发两次[重复]
Posted
技术标签:
【中文标题】长按手势识别器触发两次[重复]【英文标题】:Long Press Gesture Recognizer Fired Two times [duplicate] 【发布时间】:2015-08-28 03:51:50 【问题描述】:我对 LongPressGestureRecognizer 感到困惑。我将其中一个放在滚动视图上,但它工作了两次。当我抬起手指时,添加的方法再次调用。我想知道它只是第一次调用。应该我愿意吗?任何帮助将不胜感激,谢谢。
【问题讨论】:
显示创建和应用手势识别器的代码以及识别器处理程序的代码。 是的,当你开始印刷时它有两个叫一个,当它结束时另一个叫 【参考方案1】:先看看苹果文档是怎么说的:-
“长按手势是连续的。当允许的手指数(numberOfTouchesRequired)被按下指定的时间(minimumPressDuration)并且触摸没有超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan) )。只要手指移动,手势识别器就会转换到 Change 状态,并在任何手指抬起时结束 (UIGestureRecognizerStateEnded)。"
- (void)LongPress:(UILongPressGestureRecognizer*)sender
if (sender.state == UIGestureRecognizerStateBegan)
NSLog(@"UIGestureRecognizerStateBegan.");
//in your case add your functionality over here
else if (sender.state == UIGestureRecognizerStateEnded)
NSLog(@"UIGestureRecognizerStateEnded");
//if you want to add some more functionality when gesture got ended.
【讨论】:
【参考方案2】:UILongPressGestureRecognizer 与 UITapGestureRecognizer 不同。它包含一些状态。
- (void)viewDidLoad
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
[self.view addSubview:scrollView];
UILongPressGestureRecognizer *lpGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpHandler:)];
[scrollView addGestureRecognizer:lpGes];
- (void)lpHandler:(UILongPressGestureRecognizer *)lpGes
switch (lpGes.state)
case UIGestureRecognizerStateBegan:
NSLog(@"UILongPressGestureRecognizer: began");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"UILongPressGestureRecognizer: ended");
break;
default:
break;
对于上述代码,您将获得 2 个日志:
2015-08-28 12:22:39.084 aaaaa[50704:2339282] UILongPressGestureRecognizer: began
2015-08-28 12:22:40.687 aaaaa[50704:2339282] UILongPressGestureRecognizer: ended
【讨论】:
以上是关于长按手势识别器触发两次[重复]的主要内容,如果未能解决你的问题,请参考以下文章