77//
88
99#import " RNPanHandler.h"
10+ #import " RNGHStylusData.h"
1011
1112#if TARGET_OS_OSX
1213
@@ -31,6 +32,10 @@ @interface RNBetterPanGestureRecognizer : UIPanGestureRecognizer
3132@property (nonatomic ) CGFloat failOffsetYEnd;
3233@property (nonatomic ) CGFloat activateAfterLongPress;
3334
35+ #if !TARGET_OS_OSX && !TARGET_OS_TV
36+ @property (atomic , readonly , strong ) RNGHStylusData *stylusData;
37+ #endif
38+
3439- (id )initWithGestureHandler : (RNGestureHandler *)gestureHandler ;
3540
3641@end
@@ -80,6 +85,28 @@ - (void)setMinimumNumberOfTouches:(NSUInteger)minimumNumberOfTouches
8085}
8186#endif
8287
88+ #if !TARGET_OS_OSX && !TARGET_OS_TV
89+ - (void )tryUpdateStylusData : (UIEvent *)event
90+ {
91+ UITouch *touch = [[event allTouches ] anyObject ];
92+
93+ if (touch.type != UITouchTypePencil) {
94+ return ;
95+ } else if (_stylusData == nil ) {
96+ _stylusData = [[RNGHStylusData alloc ] init ];
97+ }
98+
99+ _stylusData.altitudeAngle = touch.altitudeAngle ;
100+ _stylusData.azimuthAngle = [touch azimuthAngleInView: nil ];
101+ _stylusData.pressure = touch.force / touch.maximumPossibleForce ;
102+
103+ CGPoint tilts = ghSpherical2tilt (_stylusData.altitudeAngle , _stylusData.azimuthAngle );
104+
105+ _stylusData.tiltX = tilts.x ;
106+ _stylusData.tiltY = tilts.y ;
107+ }
108+ #endif
109+
83110- (void )activateAfterLongPress
84111{
85112 self.state = UIGestureRecognizerStateBegan;
@@ -102,6 +129,8 @@ - (void)interactionsBegan:(NSSet *)touches withEvent:(UIEvent *)event
102129 } else {
103130 super.minimumNumberOfTouches = _realMinimumNumberOfTouches;
104131 }
132+
133+ [self tryUpdateStylusData: event];
105134#endif
106135
107136#if TARGET_OS_OSX
@@ -150,17 +179,28 @@ - (void)interactionsMoved:(NSSet *)touches withEvent:(UIEvent *)event
150179 [self setTranslation: CGPointMake (0 , 0 ) inView: self .view];
151180 }
152181 }
182+
183+ [self tryUpdateStylusData: event];
153184#endif
154185}
155186
156187- (void )interactionsEnded : (NSSet *)touches withEvent : (UIEvent *)event
157188{
158189 [_gestureHandler.pointerTracker touchesEnded: touches withEvent: event];
190+
191+ #if !TARGET_OS_TV && !TARGET_OS_OSX
192+ [self tryUpdateStylusData: event];
193+ #endif
159194}
160195
161196- (void )interactionsCancelled : (NSSet *)touches withEvent : (UIEvent *)event
162197{
163198 [_gestureHandler.pointerTracker touchesCancelled: touches withEvent: event];
199+
200+ #if !TARGET_OS_TV && !TARGET_OS_OSX
201+ [self tryUpdateStylusData: event];
202+ #endif
203+
164204 [self reset ];
165205}
166206
@@ -224,6 +264,10 @@ - (void)reset
224264 self.enabled = YES ;
225265 [super reset ];
226266 [_gestureHandler reset ];
267+
268+ #if !TARGET_OS_TV && !TARGET_OS_OSX
269+ _stylusData = nil ;
270+ #endif
227271}
228272
229273- (void )updateHasCustomActivationCriteria
@@ -405,17 +449,23 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSPanGestureRecognizer *)rec
405449 withTranslation: [recognizer translationInView: recognizer.view.window.contentView]
406450 withVelocity: [recognizer velocityInView: recognizer.view.window.contentView]
407451 withNumberOfTouches: 1
408- withPointerType: RNGestureHandlerMouse];
452+ withPointerType: RNGestureHandlerMouse
453+ withStylusData: nil ];
409454}
410455#else
411456- (RNGestureHandlerEventExtraData *)eventExtraData : (UIPanGestureRecognizer *)recognizer
412457{
413- return [RNGestureHandlerEventExtraData forPan: [recognizer locationInView: recognizer.view]
414- withAbsolutePosition: [recognizer locationInView: recognizer.view.window]
415- withTranslation: [recognizer translationInView: recognizer.view.window]
416- withVelocity: [recognizer velocityInView: recognizer.view.window]
417- withNumberOfTouches: recognizer.numberOfTouches
418- withPointerType: _pointerType];
458+ RNBetterPanGestureRecognizer *panRecognizer = (RNBetterPanGestureRecognizer *)recognizer;
459+
460+ return [RNGestureHandlerEventExtraData
461+ forPan: [recognizer locationInView: recognizer.view]
462+ withAbsolutePosition: [recognizer locationInView: recognizer.view.window]
463+ withTranslation: [recognizer translationInView: recognizer.view.window]
464+ withVelocity: [recognizer velocityInView: recognizer.view.window]
465+ withNumberOfTouches: recognizer.numberOfTouches
466+ withPointerType: _pointerType
467+ withStylusData: [panRecognizer.stylusData toDictionary ]]; // In Objective-C calling method on nil returns
468+ // nil, therefore this line does not crash.
419469}
420470#endif
421471
0 commit comments