From 629684fcda07ae25c590049e1954c4562084f95b Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Sun, 20 Jun 2021 12:03:27 -0700 Subject: [PATCH] Fix Android crashes if Bitmap is small, Fix iOS drawing in scrollView --- .../Renderer/DrawingViewRenderer.android.cs | 4 ++ .../Renderer/DrawingViewRenderer.ios.cs | 43 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.android.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.android.cs index bd94ccbae..f90b674a6 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.android.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.android.cs @@ -65,6 +65,10 @@ protected override void OnElementChanged(ElementChangedEventArgs e) protected override void OnSizeChanged(int w, int h, int oldw, int oldh) { + const int minW = 1; + const int minH = 1; + w = w < minW ? minW : w; + h = h < minH ? minH : h; base.OnSizeChanged(w, h, oldw, oldh); canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888!)!; diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.ios.cs index 581b96702..8c70e60e8 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/DrawingView/Renderer/DrawingViewRenderer.ios.cs @@ -45,6 +45,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE public override void TouchesBegan(NSSet touches, UIEvent? evt) { + SetParentTouches(false); + Element.Points.CollectionChanged -= OnPointsCollectionChanged; Element.Points.Clear(); currentPath.RemoveAllPoints(); @@ -65,16 +67,6 @@ public override void TouchesMoved(NSSet touches, UIEvent? evt) AddPointToPath(currentPoint); } - void AddPointToPath(CGPoint currentPoint) - { - currentPath.AddLineTo(currentPoint); - SetNeedsDisplay(); - Element.Points.CollectionChanged -= OnPointsCollectionChanged; - var point = currentPoint.ToPoint(); - Element.Points.Add(point); - Element.Points.CollectionChanged += OnPointsCollectionChanged; - } - public override void TouchesEnded(NSSet touches, UIEvent? evt) { UpdatePath(); @@ -86,9 +78,15 @@ public override void TouchesEnded(NSSet touches, UIEvent? evt) if (Element.ClearOnFinish) Element.Points.Clear(); + + SetParentTouches(true); } - public override void TouchesCancelled(NSSet touches, UIEvent? evt) => InvokeOnMainThread(SetNeedsDisplay); + public override void TouchesCancelled(NSSet touches, UIEvent? evt) + { + InvokeOnMainThread(SetNeedsDisplay); + SetParentTouches(true); + } public override void Draw(CGRect rect) { @@ -96,6 +94,16 @@ public override void Draw(CGRect rect) currentPath.Stroke(); } + void AddPointToPath(CGPoint currentPoint) + { + currentPath.AddLineTo(currentPoint); + SetNeedsDisplay(); + Element.Points.CollectionChanged -= OnPointsCollectionChanged; + var point = currentPoint.ToPoint(); + Element.Points.Add(point); + Element.Points.CollectionChanged += OnPointsCollectionChanged; + } + void LoadPoints() { var stylusPoints = Element.Points.Select(point => new CGPoint(point.X, point.Y)).ToList(); @@ -211,5 +219,18 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } + + void SetParentTouches(bool enabled) + { + var parent = Superview; + + while (parent != null) + { + if (parent.GetType() == typeof(ScrollViewRenderer)) + ((ScrollViewRenderer)parent).ScrollEnabled = enabled; + + parent = parent.Superview; + } + } } } \ No newline at end of file