由于 UIGestureRecognizer 正在向类对象报告,因此有几种方法可以解决此问题。 UIGestureRecognizer 并不是要在同一个视图上多次堆叠,如果这样做,除了 CPU 功率的损失和必须区分所有正在运行的识别器的大量比较代码之外,您很可能会消耗比您需要的更多的能量。但它会工作..
a)编写代码,比较其坐标和预期值,如果它们在您想要的范围内匹配,则执行您的操作。
b) 创建另一个对象,该对象仅存在于您想要的坐标中,并拥有自己的 UIGestureRecognizer。不理想,如上所述。
c) 使用UIControl 的力量,它们也继承了UIView 和UIResponders。
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;
- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event; // touch is sometimes nil if cancelTracking calls through to this.
- (void)cancelTrackingWithEvent:(nullable UIEvent *)event;
d) 在没有 UIGestureRecognizer 的情况下使用 UIView 的强大功能。 顺便说一句,这基本上也适用于 CALayers,它们没有 sendAction 方法,它们不是 UIControls。
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
BOOL inside = [super pointInside:point withEvent:event];
if (inside && event.type == UIEventTypeTouches) {
//[super sendActionsForControlEvents:UIControlEventTouchDown];
}
return inside;
}
e) 编写一些继承自 UIResponder 的类,这基本上就是 UIControls 所做的,而是使用它们的 API,因此您也可以使用触摸坐标。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
[self someCoRoutineWithTouch:touch];
}
//[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved :(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
//do some stuff per touch
}
//[super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
//do some stuff per touch
}
//[super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
请不要忘记UIGestureRecognizer 基本上是用于由触摸组成的手势,甚至是多次触摸,但通常不是主要用于捕捉触摸,尽管在很多示例中使用它们而不是编写适当的 UIControl。也不要忘记在设备边缘中,手势的识别受到手指大小的限制。