diff options
author | Samantha Houts <samantha@teamredwall.com> | 2016-08-30 11:14:13 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-08-30 11:14:13 -0700 |
commit | ab0daa194734be0e45d8582e9254c447b464b27f (patch) | |
tree | 8549bb2248d8d51987b0e017c2855622d30209d9 /Xamarin.Forms.Controls/ControlGalleryPages | |
parent | 1b7250167ff027bbd5ce3d2950a3e3495a13a5f7 (diff) | |
download | xamarin-forms-ab0daa194734be0e45d8582e9254c447b464b27f.tar.gz xamarin-forms-ab0daa194734be0e45d8582e9254c447b464b27f.tar.bz2 xamarin-forms-ab0daa194734be0e45d8582e9254c447b464b27f.zip |
[A] PanGestureRecognizer will consistently send Completed event (#313)
* Adjust gallery page for reproduction
* [A] Forward OnTouchEvent to Listener...
...and end scrolling on Up.
Diffstat (limited to 'Xamarin.Forms.Controls/ControlGalleryPages')
-rw-r--r-- | Xamarin.Forms.Controls/ControlGalleryPages/PanGestureGalleryPage.cs | 91 |
1 files changed, 62 insertions, 29 deletions
diff --git a/Xamarin.Forms.Controls/ControlGalleryPages/PanGestureGalleryPage.cs b/Xamarin.Forms.Controls/ControlGalleryPages/PanGestureGalleryPage.cs index b37e8e48..ef595b63 100644 --- a/Xamarin.Forms.Controls/ControlGalleryPages/PanGestureGalleryPage.cs +++ b/Xamarin.Forms.Controls/ControlGalleryPages/PanGestureGalleryPage.cs @@ -8,33 +8,54 @@ namespace Xamarin.Forms.Controls { public class PanGestureGalleryPage : ContentPage { - public class PanContainer : ContentView + public class PanCompleteArgs : EventArgs { - public PanContainer () + public PanCompleteArgs(string message) { Message = message; } + public string Message { - var pan = new PanGestureRecognizer - { - TouchPoints = 1 - }; + get; private set; + } + } - pan.PanUpdated += (object s, PanUpdatedEventArgs e) => - { - switch (e.StatusType) { + public class PanContainer : ContentView + { + double _x, _y; + double _currentScale = 1; + + public EventHandler<PanCompleteArgs> PanCompleted; - case GestureStatus.Started: break; + public PanContainer() + { + GestureRecognizers.Add(GetPinch()); + GestureRecognizers.Add(GetPan()); + } + PanGestureRecognizer GetPan() + { + var pan = new PanGestureRecognizer(); + pan.PanUpdated += (s, e) => + { + switch (e.StatusType) + { case GestureStatus.Running: Content.TranslationX = e.TotalX; Content.TranslationY = e.TotalY; break; - default: - Content.TranslationX = Content.TranslationY = 0; + case GestureStatus.Completed: + _x = Content.TranslationX; + _y = Content.TranslationY; + + PanCompleted?.Invoke(s, new PanCompleteArgs($"x: {_x}, y: {_y}")); break; } }; + return pan; + } - var pinch = new PinchGestureRecognizer (); + PinchGestureRecognizer GetPinch() + { + var pinch = new PinchGestureRecognizer(); double xOffset = 0; double yOffset = 0; @@ -43,14 +64,16 @@ namespace Xamarin.Forms.Controls pinch.PinchUpdated += (sender, e) => { - if (e.Status == GestureStatus.Started) { + if (e.Status == GestureStatus.Started) + { startScale = Content.Scale; Content.AnchorX = Content.AnchorY = 0; } - if (e.Status == GestureStatus.Running) { + if (e.Status == GestureStatus.Running) + { _currentScale += (e.Scale - 1) * startScale; - _currentScale = Math.Max (1, _currentScale); + _currentScale = Math.Max(1, _currentScale); var renderedX = Content.X + xOffset; var deltaX = renderedX / Width; @@ -65,32 +88,42 @@ namespace Xamarin.Forms.Controls double targetX = xOffset - (originX * Content.Width) * (_currentScale - startScale); double targetY = yOffset - (originY * Content.Height) * (_currentScale - startScale); - Content.TranslationX = targetX.Clamp (-Content.Width * (_currentScale - 1), 0); - Content.TranslationY = targetY.Clamp (-Content.Height * (_currentScale - 1), 0); + Content.TranslationX = targetX.Clamp(-Content.Width * (_currentScale - 1), 0); + Content.TranslationY = targetY.Clamp(-Content.Height * (_currentScale - 1), 0); Content.Scale = _currentScale; } - if (e.Status == GestureStatus.Completed) { + + if (e.Status == GestureStatus.Completed) + { xOffset = Content.TranslationX; yOffset = Content.TranslationY; } }; - - GestureRecognizers.Add (pinch); - - GestureRecognizers.Add (pan); + return pinch; } - - double _currentScale = 1; } - public PanGestureGalleryPage () + public PanGestureGalleryPage() { - var image = new Image { Source = "http://placehold.it/2000x2000", BackgroundColor = Color.Gray, WidthRequest = 2000, HeightRequest = 2000, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }; + var box = new Image + { + BackgroundColor = Color.Gray, + WidthRequest = 2000, + HeightRequest = 2000, + VerticalOptions = LayoutOptions.Center, + HorizontalOptions = LayoutOptions.Center + }; + + var label = new Label { Text = "Use two fingers to pinch. Use one finger to pan." }; - var panme = new PanContainer { Content = image }; + var panme = new PanContainer { Content = box }; + panme.PanCompleted += (s, e) => + { + label.Text = e.Message; + }; - Content = new StackLayout { Children = { new Label { Text = "Use two fingers to pinch. Use one finger to pan." }, panme }, Padding = new Thickness (20) }; + Content = new StackLayout { Children = { label, panme }, Padding = new Thickness(20) }; } } }
\ No newline at end of file |