gpt4 book ai didi

ios - UITapGestureRecognizer - 单击和双击

转载 作者:行者123 更新时间:2023-11-30 12:21:45 30 4
gpt4 key购买 nike

我正在尝试向 View 添加 2 个 UITapGestureRecognizers,一个用于单击事件,一个用于双击事件。单击识别器按预期工作(自行)。但我似乎无法让双击识别器工作。

已尝试尝试以下属性:cancelsTouchesInViewdelaysTouchesBegandelaysTouchesEnded 但仍然不起作用。

当我双击时,单击识别器将始终被激活,并且双击事件也会发送到 super View 。但自定义的双击识别器似乎根本没有收到通知。

文档似乎表明上述 3 个属性可以用于此目的。但我只是不确定应该设置什么值以及哪个识别器(单、双或两者)。希望熟悉这方面的人能帮忙。

以下是最新更新的代码块。

// ****** gesture recognizers ******

- (void)addSingleAndDoubleTapGestureRecognizersToView:(UIView *)view
{
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer: singleTapRecognizer];

// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
[view addGestureRecognizer: doubleTapRecognizer];
}

- (void)handleSingleTapOnView:(id)sender
{

}

- (void)handleDoubleTapOnView:(id)sender
{

}

最佳答案

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];

注意: 如果您使用 numberOfTouchesRequired 它必须是 .numberOfTouchesRequired = 1;

对于 swift

let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)

let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)

singleTapGesture.require(toFail: doubleTapGesture)

关于ios - UITapGestureRecognizer - 单击和双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44712157/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com