长按手势识别器触发了两次
Long Press Gesture Recognizer Fired Two times
我很困惑 LongPressGestureRecognizer.I 将其中一个放在滚动视图上,但它可以工作两个 times.When 我举起手指,上面添加的方法称为 again.I 想知道它只调用了第一个 time.What 我应该怎么做?任何帮助将不胜感激,谢谢。
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
先看看苹果文档是怎么说的:-
"Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted."
- (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.
}
}
我很困惑 LongPressGestureRecognizer.I 将其中一个放在滚动视图上,但它可以工作两个 times.When 我举起手指,上面添加的方法称为 again.I 想知道它只调用了第一个 time.What 我应该怎么做?任何帮助将不胜感激,谢谢。
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
先看看苹果文档是怎么说的:-
"Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted."
- (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.
}
}