ios - pressGestureRecognizer 和 touchesBegan

标签 ios cocoa-touch cocoa touchesbegan

我有以下问题。 我正在使用 UILongPressGestureRecognizer 将 UIView 置于“切换模式”。如果 UIView 处于“切换模式”,用户可以在屏幕上拖动 UIView。为了在屏幕上拖动 UIView,我使用了方法 touchesBegantouchesMovedtouchesEnded

它有效,但是:我必须抬起手指才能拖动它,因为 touchesBegan 方法已经被调用,因此不会再次调用,因此我无法拖动 UIView 环绕屏幕。

UILongPressGestureRecognizer 被触发后,有没有办法手动调用 touchesBegan(UILongPressGestureRecognizer 改变了一个 BOOL 值并且 touchesBegan 仅当此 BOOL 设置为 YES 时才有效。

最佳答案

UILongPressGestureRecognizer 是一个连续的手势识别器,因此与其求助于 touchesMovedUIPanGestureRecognizer,不如检查 UIGestureRecognizerStateChanged,例如:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [self.view addGestureRecognizer:gesture];
}

- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // user held down their finger on the screen

        // gesture started, entering the "toggle mode"
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        // user did not lift finger, but now proceeded to move finger

        // do here whatever you wanted to do in the touchesMoved
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // user lifted their finger

        // all done, leaving the "toggle mode"
    }
}

关于ios - pressGestureRecognizer 和 touchesBegan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730056/

相关文章:

ios - 将 NSString 转换为 Cocos2d-x CCString

iphone - 动态插入更多 UITextFields

macos - 在 NSPasteboard 中用第二个最新项目替换最新项目

c - 如何将 WebSockets 合并到 Cocoa 应用程序中

iphone - 在代码或数据库中传播删除?

ios - 在 Swift 中使用另一个文件中的单元格

ios - 文本搜索 PDF

iphone - 将位图表示从一个 View 复制到另一个 View 的简单或有效的方法?

ios - 计算 UITextView 中的行数,由帧大小包裹的行

ios - 使 UICollectionViewCell 完全适合可用空间