gpt4 book ai didi

iPhone - 长按发射两次相同的方法

转载 作者:行者123 更新时间:2023-12-03 20:57:46 26 4
gpt4 key购买 nike

我有一个 UIView 自定义类,并且该类创建的对象有一个长手势识别器,添加如下

UILongPressGestureRecognizer *tapAndHold =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(doStuff:)];
[tapAndHold setDelegate:self];
[tapAndHold setCancelsTouchesInView:NO];
[tapAndHold setDelaysTouchesEnded:NO];
[self addGestureRecognizer:tapAndHold];
[tapAndHold release];

问题是,每次长按对象时,doStuff 方法都会按顺序运行两次。

我错过了什么吗?谢谢

最佳答案

手势识别器可能会多次触发,因为有多个相关事件需要通知您的代表。例如,它可能会在怀疑正在进行长按时触发,并在实际长按结束时触发第二次。

手势识别器将发送对其自身的引用,您可以使用该引用来查询导致调用选择器的事件的性质。基本上,您将检查手势识别器 state 属性,并根据其值决定是否应执行您的方法代码。

更新:你的 doStuff 可能如下所示:

-(void)doStuff: (UIGestureRecognizer *)gestureRecognizer {

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
//do the actual stuff
}
}

关于iPhone - 长按发射两次相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3769933/

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