From 7dbfb96e239714ef7301791770fe4b8fca10aa4a Mon Sep 17 00:00:00 2001 From: Laura Hermanns Date: Sun, 8 Sep 2024 11:58:05 -0400 Subject: [PATCH] [iOS] Fixed pan gesture velocity in IOSCanvas. [UIPanGestureRecognizer translationInView] returns the difference of a pan-gesture from start to finish, not just the current velocity. So use the difference between the current and previous position instead. --- sources/Platform/IOS/IOSCanvas.mm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sources/Platform/IOS/IOSCanvas.mm b/sources/Platform/IOS/IOSCanvas.mm index d46dbc90d9..20106badf0 100644 --- a/sources/Platform/IOS/IOSCanvas.mm +++ b/sources/Platform/IOS/IOSCanvas.mm @@ -38,7 +38,8 @@ @interface IOSCanvasViewController : UIViewController @implementation IOSCanvasViewController { - LLGL::IOSCanvas* canvas_; + LLGL::IOSCanvas* canvas_; + CGPoint oldPanLocation_; } - (nonnull instancetype)initWithCanvas:(nonnull LLGL::IOSCanvas*)canvas; @@ -86,8 +87,11 @@ - (void)handleTapGesture:(UITapGestureRecognizer*)recognizer - (void)handlePanGesture:(UIPanGestureRecognizer*)recognizer { UIView* view = canvas_->GetUIWindow(); + const std::uint32_t numTouches = static_cast([recognizer numberOfTouches]); - const LLGL::Offset2D position = MapUIGestureLocation(recognizer, view); + CGPoint nativePosition = [recognizer locationInView:view]; + const LLGL::Offset2D position{ static_cast(nativePosition.x), static_cast(nativePosition.y) }; + switch ([recognizer state]) { case UIGestureRecognizerStateBegan: @@ -98,7 +102,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer*)recognizer case UIGestureRecognizerStateChanged: { - CGPoint velocity = [recognizer translationInView:view]; + CGPoint velocity = CGPointMake(nativePosition.x - oldPanLocation_.x, nativePosition.y - oldPanLocation_.y); canvas_->PostPanGesture(position, numTouches, velocity.x, velocity.y, LLGL::EventAction::Changed); } break; @@ -116,6 +120,8 @@ - (void)handlePanGesture:(UIPanGestureRecognizer*)recognizer } break; } + + oldPanLocation_ = nativePosition; } @end