在我的一个 View Controller 中,我有几个包含 UITapGestureRecognizer 的 View ,以及 touchesBegan
的实现。我需要优先于 touchesBegan
进行点击,因此我将手势识别器的 delaysTouchesBegan
属性设置为 YES
。这可以正常工作,但存在一个问题:手势识别器延迟 touchesBegan
的时间过长。根据documentation :
When the value of the property is YES, the window suspends delivery of touch objects in the UITouchPhaseBegan phase to the view. If the gesture recognizer subsequently recognizes its gesture, these touch objects are discarded. If the gesture recognizer, however, does not recognize its gesture, the window delivers these objects to the view in a touchesBegan:withEvent: message (and possibly a follow-up touchesMoved:withEvent: message to inform it of the touches’ current locations).
问题基本上是,当手势识别器无法识别手势并将这些对象传送到 touchesBegan
时,该操作花费的时间太长。有没有办法加快它的速度,或者只是确定它是否是点击的手势处理是密集的并且不可能缩短?
编辑:
这里有更多信息。这是我用来设置手势识别器的代码:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
tapRecognizer.delaysTouchesBegan = YES;
tapRecognizer.delegate = self;
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.someView addGestureRecognizer:tapRecognizer];
最佳答案
我会通过将 UITapGestureRecognizer
更改为 UILongPressGestureRecognizer
来解决它。这两个的标题有点误导,但使用 UILongPressGestureRecognizer
您可以设置 minimumPressDuration
:
The minimum period fingers must press on the view for the gesture to be recognized.
UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.delegate = self;
tapRecognizer.minimumPressDuration = //Up to you;
[self.someView addGestureRecognizer:tapRecognizer];
关于ios - 如何缩短 "delayTouchesBegan"delays touchesBegan 的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627715/