为了账号安全,请及时绑定邮箱和手机立即绑定

UITapGestureRecognizer-单击和双击

UITapGestureRecognizer-单击和双击

弑天下 2019-12-09 15:51:56
我正在尝试向UITapGestureRecognizers视图添加2 ,一个用于单击,一个用于双击事件。单抽头识别器正在按预期运行(单独运行)。但是我似乎无法使双击识别器正常工作。已经尝试具有类似性质的实验:cancelsTouchesInView,delaysTouchesBegan和delaysTouchesEnded,但仍然无法正常工作。当我双击时,将始终激活单击识别器,并且还将将双击事件发送到超级视图。但是自定义双击识别器似乎根本没有得到通知。文档似乎表明可以将上述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{}
查看完整描述

3 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

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;


对于斯威夫特


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)


查看完整回答
反对 回复 2019-12-09
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

Swift 3解决方案:


let singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))

singleTap.numberOfTapsRequired = 1

view.addGestureRecognizer(singleTap)


let doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))

doubleTap.numberOfTapsRequired = 2

view.addGestureRecognizer(doubleTap)


singleTap.require(toFail: doubleTap)

在代码行singleTap.require(toFail:doubleTap)中,我们强制单击以等待并确保tap事件不是双击。


查看完整回答
反对 回复 2019-12-09
?
HUWWW

TA贡献1874条经验 获得超12个赞

// ----首先,您必须分配双击手势------- //


UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap:)];


UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap:)];


[singleTap requireGestureRecognizerToFail : doubleTap];

[doubleTap setDelaysTouchesBegan : YES];

[singleTap setDelaysTouchesBegan : YES];

// -----------------------抽头数---------------- //


[doubleTap setNumberOfTapsRequired : 2];

[singleTap setNumberOfTapsRequired : 1];

// -------在视图或按钮上添加双击和单击手势-------- //


[self.view addGestureRecognizer : doubleTap];

[self.view addGestureRecognizer : singleTap];


查看完整回答
反对 回复 2019-12-09
  • 3 回答
  • 0 关注
  • 951 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信