ios - 使用 UIPanGestureRecognizer,有没有一种方法只能如此频繁地执行操作,比如在平移 x 个像素之后?

标签 ios objective-c uigesturerecognizer uipangesturerecognizer

现在,我的 UIPanGestureRecognizer 可以识别每一个平移,这很棒而且很有必要,但是当我将它用作滑动手势来增加和减少变量的值时,在方法中我只想每隔一段时间就执行一次。如果我每次检测到它时都增加 1,则该值上升得太快了。

有没有办法做类似每平移 10 个像素或类似的事情?

最佳答案

您正在寻找 translationInView: ,它会告诉您平底锅前进了多远,并且可以根据您的最小距离进行测试。此解决方案不包括您在一个方向上以等于最小距离的量来回移动的情况,但如果这对您的场景很重要,则添加起来并不难。

#define kMinimumPanDistance 100.0f

UIPanGestureRecognizer *recognizer;
CGPoint lastRecognizedInterval;

- (void)viewDidLoad {
    [super viewDidLoad];

    recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizePan:)];
    [self.view addGestureRecognizer:recognizer];
}

- (void)didRecognizePan:(UIPanGestureRecognizer*)sender {
    CGPoint thisInterval = [recognizer translationInView:self.view];

    if (abs(lastRecognizedInterval.x - thisInterval.x) > kMinimumPanDistance ||
        abs(lastRecognizedInterval.y - thisInterval.y) > kMinimumPanDistance) {

        lastRecognizedInterval = thisInterval;

        // you would add your method call here
    }
}

关于ios - 使用 UIPanGestureRecognizer,有没有一种方法只能如此频繁地执行操作,比如在平移 x 个像素之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15888276/

相关文章:

ios - 如何在 iOS 应用程序中验证 Facebook LoginSession

ios - 许多应用程序,如 Tweetbot、Facebook 等,都允许您通过向下/向上拉动来关闭图像覆盖。这是如何实现的?

ios - UIKeyboardTypeNamePhonePad - 显示选项

ios - 如何在 xcode build 命令中传递预处理器宏名称?

javascript - 如何让 react-native Picker 停留在新选择的选项上?

ios - PageControll 页面错误数 swift 4

ios - 将动态内容写入 xCode 项目 .plist

ios - 带有自签名证书的 HTTPS iOS

ios - 是否有一种内置方法可以将手势识别器(特别是平移)添加到 View 的右边缘或左边缘?

iphone - UITextView 子类点击 didSelectRowAtIndexPath