diff --git a/apple/Handlers/RNPinchHandler.m b/apple/Handlers/RNPinchHandler.m index 4cbca6ea0b..90d94c2cda 100644 --- a/apple/Handlers/RNPinchHandler.m +++ b/apple/Handlers/RNPinchHandler.m @@ -13,7 +13,12 @@ #if !TARGET_OS_TV #if TARGET_OS_OSX -@interface RNBetterPinchRecognizer : NSMagnificationGestureRecognizer +@interface RNBetterPinchRecognizer : NSMagnificationGestureRecognizer { + CGFloat prevMagnification; + NSTimeInterval prevTime; +} + +@property (nonatomic, readonly) CGFloat velocity; #else @interface RNBetterPinchRecognizer : UIPinchGestureRecognizer #endif @@ -31,6 +36,10 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler if ((self = [super initWithTarget:self action:@selector(handleGesture:)])) { _gestureHandler = gestureHandler; } +#if TARGET_OS_OSX + prevMagnification = 0; + prevTime = 0; +#endif return self; } @@ -67,28 +76,28 @@ - (void)interactionsCancelled:(NSSet *)touches withEvent:(UIEvent *)event } #if TARGET_OS_OSX -- (void)touchesBeganWithEvent:(NSEvent *)event -{ - [super touchesBeganWithEvent:event]; - [self interactionsBegan:[NSSet setWithObject:event] withEvent:event]; -} - -- (void)touchesMovedWithEvent:(NSEvent *)event -{ - [super touchesMovedWithEvent:event]; - [self interactionsMoved:[NSSet setWithObject:event] withEvent:event]; -} - -- (void)touchesEndedWithEvent:(NSEvent *)event -{ - [super touchesEndedWithEvent:event]; - [self interactionsEnded:[NSSet setWithObject:event] withEvent:event]; -} +- (void)magnifyWithEvent:(NSEvent *)event +{ + [super magnifyWithEvent:event]; + + switch (self.state) { + case NSGestureRecognizerStateBegan: + [self interactionsBegan:[NSSet setWithObject:event] withEvent:event]; + break; + case NSGestureRecognizerStateChanged: + [self interactionsMoved:[NSSet setWithObject:event] withEvent:event]; + break; + case NSGestureRecognizerStateEnded: + [self interactionsEnded:[NSSet setWithObject:event] withEvent:event]; + break; + case NSGestureRecognizerStateCancelled: + [self interactionsCancelled:[NSSet setWithObject:event] withEvent:event]; + break; + } -- (void)touchesCancelledWithEvent:(NSEvent *)event -{ - [super touchesCancelledWithEvent:event]; - [self interactionsCancelled:[NSSet setWithObject:event] withEvent:event]; + _velocity = (self.magnification - prevMagnification) / ((event.timestamp - prevTime) * 1000); + prevMagnification = self.magnification; + prevTime = event.timestamp; } #else - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event @@ -145,7 +154,7 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSMagnificationGestureRecogn { return [RNGestureHandlerEventExtraData forPinch:recognizer.magnification withFocalPoint:[recognizer locationInView:recognizer.view] - withVelocity:1 + withVelocity:((RNBetterPinchRecognizer *)recognizer).velocity withNumberOfTouches:2]; } #else diff --git a/apple/Handlers/RNRotationHandler.m b/apple/Handlers/RNRotationHandler.m index dfeedb3f8d..61d6366238 100644 --- a/apple/Handlers/RNRotationHandler.m +++ b/apple/Handlers/RNRotationHandler.m @@ -11,10 +11,16 @@ #if !TARGET_OS_TV #if TARGET_OS_OSX -@interface RNBetterRotationRecognizer : NSRotationGestureRecognizer +@interface RNBetterRotationRecognizer : NSRotationGestureRecognizer { + CGFloat prevRotation; + NSTimeInterval prevTime; +} + +@property (nonatomic, readonly) CGFloat velocity; #else @interface RNBetterRotationRecognizer : UIRotationGestureRecognizer #endif + - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler; @end @@ -28,6 +34,10 @@ - (id)initWithGestureHandler:(RNGestureHandler *)gestureHandler if ((self = [super initWithTarget:self action:@selector(handleGesture:)])) { _gestureHandler = gestureHandler; } +#if TARGET_OS_OSX + prevRotation = 0; + prevTime = 0; +#endif return self; } @@ -60,28 +70,28 @@ - (void)interactionsCancelled:(NSSet *)touches withEvent:(UIEvent *)event } #if TARGET_OS_OSX -- (void)touchesBeganWithEvent:(NSEvent *)event -{ - [super touchesBeganWithEvent:event]; - [self interactionsBegan:[NSSet setWithObject:event] withEvent:event]; -} - -- (void)touchesMovedWithEvent:(NSEvent *)event -{ - [super touchesMovedWithEvent:event]; - [self interactionsMoved:[NSSet setWithObject:event] withEvent:event]; -} - -- (void)touchesEndedWithEvent:(NSEvent *)event -{ - [super touchesEndedWithEvent:event]; - [self interactionsEnded:[NSSet setWithObject:event] withEvent:event]; -} +- (void)rotateWithEvent:(NSEvent *)event +{ + [super rotateWithEvent:event]; + + switch (self.state) { + case NSGestureRecognizerStateBegan: + [self interactionsBegan:[NSSet setWithObject:event] withEvent:event]; + break; + case NSGestureRecognizerStateChanged: + [self interactionsMoved:[NSSet setWithObject:event] withEvent:event]; + break; + case NSGestureRecognizerStateEnded: + [self interactionsEnded:[NSSet setWithObject:event] withEvent:event]; + break; + case NSGestureRecognizerStateCancelled: + [self interactionsCancelled:[NSSet setWithObject:event] withEvent:event]; + break; + } -- (void)touchesCancelledWithEvent:(NSEvent *)event -{ - [super touchesCancelledWithEvent:event]; - [self interactionsCancelled:[NSSet setWithObject:event] withEvent:event]; + _velocity = (self.rotation - prevRotation) / ((event.timestamp - prevTime) * 1000); + prevRotation = self.rotation; + prevTime = event.timestamp; } #else - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event @@ -138,7 +148,7 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSRotationGestureRecognizer { return [RNGestureHandlerEventExtraData forRotation:recognizer.rotation withAnchorPoint:[recognizer locationInView:recognizer.view] - withVelocity:1 + withVelocity:((RNBetterRotationRecognizer *)recognizer).velocity withNumberOfTouches:2]; } #else