gpt4 book ai didi

ios - UITapGestureRecognizer - 单击和双击

转载 作者:IT王子 更新时间:2023-10-29 07:26:43 29 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/8876202/

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