From c8ccf6089c496c06b2ac2148f37497a9f82fe07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 29 Jul 2020 13:30:10 +0200 Subject: [PATCH] [iOS] Change Button state if the touch-up event occurs outside (#11441) fixes #11430 * Added repro sample * Fixed the issue * Updated the sample Co-authored-by: Samantha Houts --- .../Issue11430.cs | 55 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../Renderers/ButtonElementManager.cs | 5 ++ .../Renderers/ButtonRenderer.cs | 7 +++ 4 files changed, 68 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11430.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11430.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11430.cs new file mode 100644 index 000000000..5f06b078c --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11430.cs @@ -0,0 +1,55 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +using Xamarin.Forms.Core.UITests; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ +#if UITEST + [Category(UITestCategories.Frame)] +#endif + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 11430, + "[Bug] [iOS] Button stays in Pressed state if the touch-up event occurs outside", + PlatformAffected.iOS)] + public class Issue11430 : TestContentPage + { + public Issue11430() + { + } + + protected override void Init() + { + Title = "Issue 11430"; + + var layout = new StackLayout(); + + var instructions = new Label + { + Padding = 12, + BackgroundColor = Color.Black, + TextColor = Color.White, + Text = "Tap a Button, drag your finger outside the Button and lift up your finger. If the Button state is Normal, the test has passed." + }; + + var button = new Button + { + Text = "Click Me" + }; + + layout.Children.Add(instructions); + layout.Children.Add(button); + + Content = layout; + + button.Clicked += (sender, args) => + { + DisplayAlert("Issue 11430", "Button Clicked.", "Ok"); + }; + } + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 9ef45af68..1fac1c426 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -1440,6 +1440,7 @@ + diff --git a/Xamarin.Forms.Platform.iOS/Renderers/ButtonElementManager.cs b/Xamarin.Forms.Platform.iOS/Renderers/ButtonElementManager.cs index 4ce29b048..225e63d48 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/ButtonElementManager.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/ButtonElementManager.cs @@ -85,5 +85,10 @@ namespace Xamarin.Forms.Platform.iOS element?.SendReleased(); element?.SendClicked(); } + + internal static void OnButtonTouchUpOutside(IButtonController element) + { + element?.SendReleased(); + } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs index 0646d0664..2e029de9e 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs @@ -62,6 +62,7 @@ namespace Xamarin.Forms.Platform.iOS if (Control != null) { Control.TouchUpInside -= OnButtonTouchUpInside; + Control.TouchUpOutside -= OnButtonTouchUpOutside; Control.TouchDown -= OnButtonTouchDown; BorderElementManager.Dispose(this); _buttonLayoutManager?.Dispose(); @@ -93,6 +94,7 @@ namespace Xamarin.Forms.Platform.iOS _buttonTextColorDefaultDisabled = Control.TitleColor(UIControlState.Disabled); Control.TouchUpInside += OnButtonTouchUpInside; + Control.TouchUpOutside += OnButtonTouchUpOutside; Control.TouchDown += OnButtonTouchDown; } @@ -147,6 +149,11 @@ namespace Xamarin.Forms.Platform.iOS ButtonElementManager.OnButtonTouchUpInside(this.Element); } + void OnButtonTouchUpOutside(object sender, EventArgs eventArgs) + { + ButtonElementManager.OnButtonTouchUpOutside(this.Element); + } + void OnButtonTouchDown(object sender, EventArgs eventArgs) { ButtonElementManager.OnButtonTouchDown(this.Element);