Skip to content

Commit

Permalink
[iOS] Fixed pan gesture velocity in IOSCanvas.
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
LukasBanana committed Sep 8, 2024
1 parent 95ff758 commit 7dbfb96
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions sources/Platform/IOS/IOSCanvas.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ @interface IOSCanvasViewController : UIViewController

@implementation IOSCanvasViewController
{
LLGL::IOSCanvas* canvas_;
LLGL::IOSCanvas* canvas_;
CGPoint oldPanLocation_;
}

- (nonnull instancetype)initWithCanvas:(nonnull LLGL::IOSCanvas*)canvas;
Expand Down Expand Up @@ -86,8 +87,11 @@ - (void)handleTapGesture:(UITapGestureRecognizer*)recognizer
- (void)handlePanGesture:(UIPanGestureRecognizer*)recognizer
{
UIView* view = canvas_->GetUIWindow();

const std::uint32_t numTouches = static_cast<std::uint32_t>([recognizer numberOfTouches]);
const LLGL::Offset2D position = MapUIGestureLocation(recognizer, view);
CGPoint nativePosition = [recognizer locationInView:view];
const LLGL::Offset2D position{ static_cast<std::int32_t>(nativePosition.x), static_cast<std::int32_t>(nativePosition.y) };

switch ([recognizer state])
{
case UIGestureRecognizerStateBegan:
Expand All @@ -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;
Expand All @@ -116,6 +120,8 @@ - (void)handlePanGesture:(UIPanGestureRecognizer*)recognizer
}
break;
}

oldPanLocation_ = nativePosition;
}

@end
Expand Down

0 comments on commit 7dbfb96

Please sign in to comment.