UITapGestureRecognizer-使它在触地而不是触地工作吗?

我使用tap事件的时间非常敏感,因此我很好奇是否有可能在用户简单按下时激活UITapGestureRecognizer,而不是要求他们同时按下吗?



慕码人2483693
浏览 564回答 3
3回答

一只斗牛犬

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

三国纷争

使用UILongPressGestureRecognizer并将其设置minimumPressDuration为0。在UIGestureRecognizerStateBegan状态期间,它将像着陆一样。对于Swift 4func setupTap() {&nbsp; &nbsp; let touchDown = UILongPressGestureRecognizer(target:self, action: #selector(didTouchDown))&nbsp; &nbsp; touchDown.minimumPressDuration = 0&nbsp; &nbsp; view.addGestureRecognizer(touchDown)}@objc func didTouchDown(gesture: UILongPressGestureRecognizer) {&nbsp; &nbsp; if gesture.state == .began {&nbsp; &nbsp; &nbsp; &nbsp; doSomething()&nbsp; &nbsp; }}对于Objective-C例:-(void)setupLongPress{&nbsp; &nbsp;self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];&nbsp; &nbsp;self.longPress.minimumPressDuration = 0;&nbsp; &nbsp;[self.view addGestureRecognizer:self.longPress];}-(void)didLongPress:(UILongPressGestureRecognizer *)gesture{&nbsp; &nbsp;if (gesture.state == UIGestureRecognizerStateBegan){&nbsp; &nbsp; &nbsp; [self doSomething];&nbsp; &nbsp;}}

慕容森

Swift(无子类)这是Swift版本,类似于Rob Caraway的Objective-C答案。想法是使用minimumPressDuration设置为零的长按手势识别器,而不是使用轻击手势识别器。这是因为长按手势识别器报告触摸开始事件,而点击手势没有报告。import UIKitclass ViewController: UIViewController {&nbsp; &nbsp; @IBOutlet weak var myView: UIView!&nbsp; &nbsp; override func viewDidLoad() {&nbsp; &nbsp; &nbsp; &nbsp; super.viewDidLoad()&nbsp; &nbsp; &nbsp; &nbsp; // Add "long" press gesture recognizer&nbsp; &nbsp; &nbsp; &nbsp; let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))&nbsp; &nbsp; &nbsp; &nbsp; tap.minimumPressDuration = 0&nbsp; &nbsp; &nbsp; &nbsp; myView.addGestureRecognizer(tap)&nbsp; &nbsp; }&nbsp; &nbsp; // called by gesture recognizer&nbsp; &nbsp; @objc func tapHandler(gesture: UITapGestureRecognizer) {&nbsp; &nbsp; &nbsp; &nbsp; // handle touch down and touch up events separately&nbsp; &nbsp; &nbsp; &nbsp; if gesture.state == .began {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do something...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("tap down")&nbsp; &nbsp; &nbsp; &nbsp; } else if gesture.state == .ended { // optional for touch up event catching&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do something else...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("tap up")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP