Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rotation and pinch velocity on macOS #2706

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions apple/Handlers/RNPinchHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
prevMagnification = self.magnification;
prevTime = event.timestamp;
}
#else
- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
Expand Down Expand Up @@ -144,7 +153,7 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSMagnificationGestureRecogn
{
return [RNGestureHandlerEventExtraData forPinch:recognizer.magnification
withFocalPoint:[recognizer locationInView:recognizer.view]
withVelocity:1
withVelocity:((RNBetterPinchRecognizer *)recognizer).velocity
withNumberOfTouches:2];
}
#else
Expand Down
56 changes: 33 additions & 23 deletions apple/Handlers/RNRotationHandler.m
jfedak marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delta should also be multiplied by 1000, right?

prevRotation = self.rotation;
prevTime = event.timestamp;
}
#else
- (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
Expand Down Expand Up @@ -137,7 +147,7 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSRotationGestureRecognizer
{
return [RNGestureHandlerEventExtraData forRotation:recognizer.rotation
withAnchorPoint:[recognizer locationInView:recognizer.view]
withVelocity:1
withVelocity:((RNBetterRotationRecognizer *)recognizer).velocity
withNumberOfTouches:2];
}
#else
Expand Down
Loading