3

我只需要知道是否有一种方法可以在一个 UIGestureRecognizer 实例中捕获所有类型的手势。

示例:我有一个 UIView,我必须检测对其进行的任何类型的点击,而无需为每种类型的手势创建实例

有没有办法做到这一点 ?

谢谢,

4

2 回答 2

6

当然是,自己处理低级 UIView 事件(iOS 事件处理指南):

Responding to Touch Events
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
Responding to Motion Events
– motionBegan:withEvent:
– motionEnded:withEvent:
– motionCancelled:withEvent:
于 2013-03-13T08:32:33.470 回答
3

您可以子类化UIGestureRecognizer该类并将其内部状态更改UIGestureRecognizerStateRecognized为何时开始触摸。

示例代码:

@interface UITouchGestureRecognizer : UIGestureRecognizer

@end


#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation UITouchGestureRecognizer

- (void) reset
{
    [super reset];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    self.state = UIGestureRecognizerStateRecognized;
}

@end
于 2013-11-05T19:45:57.673 回答