Hey guys,
I'm struggling with building a custom UIGestureRecognizer using 3D Touch. I subclassedUIGestureRecognizer according to the documentation, but regrettably any touch, whether its force passes the specified threshold or not, sends an action message to the target. Even if the gesture isn't interpreted successfully! Whensoever I tap, the target's selector is executed. My implementation of the overridden methods reads as follows:
- (id)initWithTarget:(id)target action:(SEL)action threshold:(CGFloat)threshold {
self = [super initWithTarget:target action:action];
if (self) {
[self setThreshold:threshold];
}
return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
self.state = UIGestureRecognizerStateBegan;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = touches.anyObject;
if (touch) {
CGFloat recognizedForceNormalized = touch.force/touch.maximumPossibleForce;
if (recognizedForceNormalized < 1.0 && recognizedForceNormalized <= self.getThreshold) {
// is the recognized force (normalized) smaller 1.0 (our max. possible value) and
// unequal the specified threshold?
self.state = UIGestureRecognizerStateChanged;
}
if (recognizedForceNormalized >= self.getThreshold) {
self.state = UIGestureRecognizerStateRecognized;
}
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
self.state = UIGestureRecognizerStateEnded;
}
Does anyone of you have an idea what the matter with this implementation is? I really appreciate any help you can provide!!
Kind regards,
Alex