Skip to content

Commit

Permalink
Merge pull request #30 from IeuanWalker/Fix
Browse files Browse the repository at this point in the history
Potential fix for random crashes
  • Loading branch information
IeuanWalker authored Jun 1, 2023
2 parents c042352 + d62ad80 commit 894ae82
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Demo/App.Android/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Scr/StateButton.Android/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 58 additions & 29 deletions Scr/StateButton.iOS/StateButtonRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Foundation;
using StateButton.iOS;
using System;
using System.Linq;
using UIKit;
using Xamarin.Forms;
Expand All @@ -17,67 +18,95 @@ public class StateButtonRenderer : FrameRenderer

protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
try
{
base.OnElementChanged(e);

AccessibilityTraits = UIAccessibilityTrait.Button;
AccessibilityTraits = UIAccessibilityTrait.Button;

if (e.OldElement != null) return;
if (e.OldElement != null) return;

if (!e.NewElement.GestureRecognizers.Any())
return;
if (!e.NewElement.GestureRecognizers.Any())
return;

if (e.NewElement.GestureRecognizers.All(x => x.GetType() != typeof(TouchGestureRecognizer)))
return;
if (e.NewElement.GestureRecognizers.All(x => !(x is TouchGestureRecognizer)))
return;

AddGestureRecognizer(new UITapGestureRecognizer(() =>
{
foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
AddGestureRecognizer(new UITapGestureRecognizer(() =>
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
touchGestureRecognizer.Clicked();
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
{
touchGestureRecognizer.Clicked();
}
}
}
}));
}));
}
catch (Exception)
{
// ignored
}
}

public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
try
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
base.TouchesMoved(touches, evt);

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
touchGestureRecognizer.Released();
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
{
touchGestureRecognizer.Released();
}
}
}
catch (Exception)
{
// ignored
}
}

public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
try
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
base.TouchesBegan(touches, evt);

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
touchGestureRecognizer.Pressed();
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
{
touchGestureRecognizer.Pressed();
}
}
}
catch (Exception)
{
// ignored
}
}

public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
base.TouchesCancelled(touches, evt);

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
try
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
base.TouchesCancelled(touches, evt);

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
touchGestureRecognizer.Released();
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
{
touchGestureRecognizer.Released();
}
}
}
catch (Exception)
{
// ignored
}
}
}
}

0 comments on commit 894ae82

Please sign in to comment.