【问题标题】:iOS project:Why the long press gesture is so easy to be triggered?iOS项目:为什么长按手势这么容易被触发?
【发布时间】:2016-11-13 11:58:52
【问题描述】:

正如标题所说,当我更新Xcode8时,在我的项目中,长按手势很容易触发,即使我点击屏幕,它也会调用!而且键盘也有这个问题。当我输入一个单词时,Xcode 会打印如下信息:

[UIWindow endDisablingInterfaceAutorotationAnimated:] called on <UIRemoteKeyboardWindow: 0x100ffb940; frame = (0 0; 414 736); opaque = NO; autoresize = W+H; layer = <UIWindowLayer: 0x17042d700>> without matching -beginDisablingInterfaceAutorotation. Ignoring.

在一个视图中,我添加了一个点击手势和长按手势,当我点击(只是触摸)时,触发的手势是长按,而不是点击手势。这个问题我没有在任何地方搜索过,所以我来这里寻求您的帮助。 (原谅我,我的英语很糟糕)

我贴两张图让你看清楚。

这是我的代码:

UILongPressGestureRecognizer * lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lp:)];
lp.minimumPressDuration = 1.0f;
[_imageView addGestureRecognizer:lp];
- (void)lp:(UILongPressGestureRecognizer*)lp {
    if (lp.state == UIGestureRecognizerStateBegan) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(longPressImage)]) {
            [self.delegate longPressImage];
        }
    }
}

另外,这个问题发生在某些设备上,而不是所有设备上。一个设备总是发生,其他只是在某个时候发生,当我再次构建项目时,它只是 OKO__O "...

【问题讨论】:

  • 请张贴代码。
  • 抱歉,这个问题很难理解。
  • 使用相关代码更新您的问题,展示您如何创建和设置长按手势以及如何处理其事件。
  • @shallowThought UILongPressGestureRecognizer * lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lp:)]; lp.minimumPressDuration = 1.0f; [_imageView addGestureRecognizer:lp]; - (void)lp:(UILongPressGestureRecognizer*)lp { if (lp.state == UIGestureRecognizerStateBegan) { if (self.delegate &amp;&amp; [self.delegate respondsToSelector:@selector(longPressImage)]) { [self.delegate longPressImage]; } } }
  • @rmaddy UILongPressGestureRecognizer * lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lp:)]; lp.minimumPressDuration = 1.0f; [_imageView addGestureRecognizer:lp]; - (void)lp:(UILongPressGestureRecognizer*)lp { if (lp.state == UIGestureRecognizerStateBegan) { if (self.delegate &amp;&amp; [self.delegate respondsToSelector:@selector(longPressImage)]) { [self.delegate longPressImage]; } } }

标签: ios objective-c


【解决方案1】:

您应该检查UIGestureRecognizerStateEnded 而不是UIGestureRecognizerStateBeganUIGestureRecognizerStateBegan 当触摸被识别为某个手势的开始时触发。此手势不是您要查找的长按所必需的。仅在识别器状态更改为UIGestureRecognizerStateEnded 时按预期执行长按。

- (void)lp:(UILongPressGestureRecognizer*)lp {
    if (lp.state == UIGestureRecognizerStateEnded) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(longPressImage)]) {
            [self.delegate longPressImage];
        }
    }
}

【讨论】:

  • 感谢您的回答。但我认为这不是解决方案,我尝试了这个,但它不起作用。在我看来,手势不是原因,可能是我的原因在某处使用一些运行时。无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多