一只斗牛犬
创建您的自定义TouchDownGestureRecognizer子类,并在touches中实现手势。TouchDownGestureRecognizer.h#import <UIKit/UIKit.h>@interface TouchDownGestureRecognizer : UIGestureRecognizer@endTouchDownGestureRecognizer.m#import "TouchDownGestureRecognizer.h"#import <UIKit/UIGestureRecognizerSubclass.h>@implementation TouchDownGestureRecognizer-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ if (self.state == UIGestureRecognizerStatePossible) { self.state = UIGestureRecognizerStateRecognized; }}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ self.state = UIGestureRecognizerStateFailed;}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ self.state = UIGestureRecognizerStateFailed;}@end实施:#import "TouchDownGestureRecognizer.h" TouchDownGestureRecognizer *touchDown = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchDown:)]; [yourView addGestureRecognizer:touchDown];-(void)handleTouchDown:(TouchDownGestureRecognizer *)touchDown{ NSLog(@"Down");}迅捷的实现:import UIKitimport UIKit.UIGestureRecognizerSubclassclass TouchDownGestureRecognizer: UIGestureRecognizer{ override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) { if self.state == .Possible { self.state = .Recognized } } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) { self.state = .Failed } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) { self.state = .Failed }}这是2017年要粘贴的Swift语法:import UIKit.UIGestureRecognizerSubclassclass SingleTouchDownGestureRecognizer: UIGestureRecognizer { override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { if self.state == .possible { self.state = .recognized } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { self.state = .failed } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) { self.state = .failed }}请注意,这是的替代品UITap。所以在像这样的代码中func add(tap v:UIView, _ action:Selector) { let t = UITapGestureRecognizer(target: self, action: action) v.addGestureRecognizer(t)}您可以安全地交换到...。func add(hairtriggerTap v:UIView, _ action:Selector) { let t = SingleTouchDownGestureRecognizer(target: self, action: action) v.addGestureRecognizer(t)}测试表明它不会被多次调用。它可以作为替代品。您可以在两个通话之间进行交换。