Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit ee053fe

Browse files
authored
[iOS] Block the scroll if is swiping a SwipeView (#11949) fixes #11651 fixes #11932
* Block the scroll if is swiping a SwipeView on iOS * Updated MinimumOpenSwipeThresholdPercentage
1 parent e2c0d37 commit ee053fe

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ public static UIImage ConvertToImage(this UIView view)
3535
}
3636
#endif
3737

38+
internal static T GetParentOfType<T>(this UIView view)
39+
where T : class
40+
{
41+
if (view is T t)
42+
return t;
43+
44+
while (view != null)
45+
{
46+
T parent = view.Superview as T;
47+
if (parent != null)
48+
return parent;
49+
50+
view = view.Superview;
51+
}
52+
53+
return default(T);
54+
}
55+
3856
public static IEnumerable<UIView> Descendants(this UIView self)
3957
{
4058
if (self.Subviews == null)

Xamarin.Forms.Platform.iOS/Renderers/SwipeViewRenderer.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Xamarin.Forms.Platform.iOS
1313
{
1414
public class SwipeViewRenderer : ViewRenderer<SwipeView, UIView>
1515
{
16+
const float MinimumOpenSwipeThresholdPercentage = 0.15f; // 15%
1617
const float OpenSwipeThresholdPercentage = 0.6f; // 60%
1718
const double SwipeThreshold = 250;
1819
const double SwipeItemWidth = 100;
@@ -38,6 +39,7 @@ public class SwipeViewRenderer : ViewRenderer<SwipeView, UIView>
3839
double _previousScrollY;
3940
int _previousFirstVisibleIndex;
4041
bool _isSwipeEnabled;
42+
bool _isScrollEnabled;
4143
bool _isResettingSwipe;
4244
bool _isOpen;
4345
bool _isDisposed;
@@ -48,6 +50,7 @@ public SwipeViewRenderer()
4850
SwipeView.VerifySwipeViewFlagEnabled(nameof(SwipeViewRenderer));
4951

5052
_swipeItems = new Dictionary<ISwipeItem, object>();
53+
_isScrollEnabled = true;
5154

5255
_tapGestureRecognizer = new UITapGestureRecognizer(HandleTap)
5356
{
@@ -763,7 +766,10 @@ void ProcessTouchMove(CGPoint point)
763766
UpdateIsOpen(_swipeOffset != 0);
764767

765768
if (Math.Abs(_swipeOffset) > double.Epsilon)
769+
{
770+
IsParentScrollEnabled(false);
766771
Swipe();
772+
}
767773

768774
RaiseSwipeChanging();
769775
}
@@ -779,6 +785,7 @@ void ProcessTouchUp(CGPoint point)
779785
return;
780786

781787
_isSwiping = false;
788+
IsParentScrollEnabled(true);
782789

783790
RaiseSwipeEnded();
784791

@@ -788,6 +795,24 @@ void ProcessTouchUp(CGPoint point)
788795
ValidateSwipeThreshold();
789796
}
790797

798+
void IsParentScrollEnabled(bool scrollEnabled)
799+
{
800+
var swipeThresholdPercent = MinimumOpenSwipeThresholdPercentage * GetSwipeThreshold();
801+
802+
if (Math.Abs(_swipeOffset) < swipeThresholdPercent)
803+
return;
804+
805+
if (scrollEnabled == _isScrollEnabled)
806+
return;
807+
808+
_isScrollEnabled = scrollEnabled;
809+
810+
var parent = this.GetParentOfType<UIScrollView>();
811+
812+
if (parent != null)
813+
parent.ScrollEnabled = _isScrollEnabled;
814+
}
815+
791816
bool CanProcessTouchSwipeItems(CGPoint point)
792817
{
793818
// We only invoke the SwipeItem command if we tap on the SwipeItems area

0 commit comments

Comments
 (0)