Créez votre sous-classe personnalisée TouchDownGestureRecognizer et implémentez le geste dans touchesBegan:
TouchDownGestureRecognizer.h
#import
@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end
TouchDownGestureRecognizer.m
#import "TouchDownGestureRecognizer.h"
#import
@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
implémentation:
#import "TouchDownGestureRecognizer.h"
TouchDownGestureRecognizer *touchDown = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchDown:)];
[yourView addGestureRecognizer:touchDown];
-(void)handleTouchDown:(TouchDownGestureRecognizer *)touchDown{
NSLog(@"Down");
}
Implémentation en Swift:
import UIKit
import UIKit.UIGestureRecognizerSubclass
class TouchDownGestureRecognizer: UIGestureRecognizer
{
override func touchesBegan(touches: Set, withEvent event: UIEvent)
{
if self.state == .Possible
{
self.state = .Recognized
}
}
override func touchesMoved(touches: Set, withEvent event: UIEvent)
{
self.state = .Failed
}
override func touchesEnded(touches: Set, withEvent event: UIEvent)
{
self.state = .Failed
}
}
Voici la syntaxe Swift pour 2017 à coller:
import UIKit.UIGestureRecognizerSubclass
class SingleTouchDownGestureRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set, with event: UIEvent) {
if self.state == .possible {
self.state = .recognized
}
}
override func touchesMoved(_ touches: Set, with event: UIEvent) {
self.state = .failed
}
override func touchesEnded(_ touches: Set, with event: UIEvent) {
self.state = .failed
}
}
Notez que c'est un remplacement direct pour UITap
. Donc dans du code comme...
func add(tap v:UIView, _ action:Selector) {
let t = UITapGestureRecognizer(target: self, action: action)
v.addGestureRecognizer(t)
}
vous pouvez en toute sécurité passer à....
func add(hairtriggerTap v:UIView, _ action:Selector) {
let t = SingleTouchDownGestureRecognizer(target: self, action: action)
v.addGestureRecognizer(t)
}
Les tests montrent qu'elle ne sera pas appelée plus d'une fois. Cela fonctionne comme un remplacement direct; vous pouvez simplement alterner entre les deux appels.
1 votes
Si cela aide, UITouch a une méthode touchesStarted. Mais cela n'utilise pas de reconnaissants de gestes, comme vous l'avez demandé.