From d9d334cf1dd76464e8b2c89583ec7ae88a317721 Mon Sep 17 00:00:00 2001 From: Frank <472730949@qq.com> Date: Mon, 26 Apr 2021 18:03:04 +0800 Subject: [PATCH 1/2] Fix YYTextView selection bug that selection cannot be done when selecting one more line (trigger scrolling action) --- YYText/YYTextView.h | 4 ++++ YYText/YYTextView.m | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/YYText/YYTextView.h b/YYText/YYTextView.h index db5a275a..4893f806 100644 --- a/YYText/YYTextView.h +++ b/YYText/YYTextView.h @@ -401,6 +401,10 @@ IB_DESIGNABLE @end #endif // !TARGET_INTERFACE_BUILDER +@interface YYTextView() +/// Default is nil. +/// Only for YYTextView embedded in a `ScrollView`. Set this to block superview to respond PanGesture or scrolling action. +@property (nullable, nonatomic, weak) UIScrollView *interactiveSuperScrollView; // Notifications, see UITextView's documentation for more information. UIKIT_EXTERN NSString *const YYTextViewTextDidBeginEditingNotification; diff --git a/YYText/YYTextView.m b/YYText/YYTextView.m index 01b1629a..11092e5d 100644 --- a/YYText/YYTextView.m +++ b/YYText/YYTextView.m @@ -855,11 +855,11 @@ - (void)_trackDidLongPress { [self _removeHighlightAnimated:NO]; if (_state.trackingTouch) { if (_state.trackingGrabber) { - self.panGestureRecognizer.enabled = NO; + [self _disablePanGestures]; [self _hideMenu]; [self _showMagnifierRanged]; } else if (self.isFirstResponder){ - self.panGestureRecognizer.enabled = NO; + [self _disablePanGestures]; _selectionView.caretBlinks = NO; _state.trackingCaret = YES; CGPoint trackingPoint = [self _convertPointToLayout:_trackingPoint]; @@ -884,7 +884,7 @@ - (void)_trackDidLongPress { [self _showMagnifierCaret]; } } else if (self.selectable) { - self.panGestureRecognizer.enabled = NO; + [self _disablePanGestures]; _state.trackingPreSelect = YES; _state.selectedWithoutEdit = NO; [self _updateTextRangeByTrackingPreSelect]; @@ -1030,6 +1030,7 @@ - (void)_endTouchTracking { [self _updateSelectionView]; self.panGestureRecognizer.enabled = self.scrollEnabled; + self.interactiveSuperScrollView.scrollEnabled = YES; } /// Start a timer to fix the selection dot. @@ -1049,6 +1050,12 @@ - (void)_endSelectionDotFixTimer { _selectionDotFixTimer = nil; } +// Disable scrollView panGesture and interactive superview scroll action +- (void)_disablePanGestures { + self.panGestureRecognizer.enabled = NO; // disable scroll view + self.interactiveSuperScrollView.scrollEnabled = NO; // disable interactive superview +} + /// If it shows selection grabber and this view was moved by super view, /// update the selection dot in window. - (void)_fixSelectionDot { @@ -2500,6 +2507,7 @@ - (CGSize)sizeThatFits:(CGSize)size { return layout.textBoundingSize; } + #pragma mark - Override UIResponder - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { @@ -2534,7 +2542,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self _showHighlightAnimated:NO]; } else { if ([_selectionView isGrabberContainsPoint:point]) { // track grabber - self.panGestureRecognizer.enabled = NO; // disable scroll view + [self _disablePanGestures]; [self _hideMenu]; _state.trackingGrabber = [_selectionView isStartGrabberContainsPoint:point] ? kStart : kEnd; _magnifierRangedOffset = [self _getMagnifierRangedOffset]; @@ -2542,7 +2550,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (_selectedTextRange.asRange.length == 0 && self.isFirstResponder) { if ([_selectionView isCaretContainsPoint:point]) { // track caret _state.trackingCaret = YES; - self.panGestureRecognizer.enabled = NO; // disable scroll view + [self _disablePanGestures]; } } } @@ -2583,7 +2591,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { } else { _trackingRange = _selectedTextRange; if (_state.trackingGrabber) { - self.panGestureRecognizer.enabled = NO; + [self _disablePanGestures]; [self _hideMenu]; [self _updateTextRangeByTrackingGrabber]; showMagnifierRanged = YES; @@ -2596,11 +2604,11 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [self _hideMenu]; if (_verticalForm) { if (_state.touchMoved == kTop || _state.touchMoved == kBottom) { - self.panGestureRecognizer.enabled = NO; + [self _disablePanGestures]; } } else { if (_state.touchMoved == kLeft || _state.touchMoved == kRight) { - self.panGestureRecognizer.enabled = NO; + [self _disablePanGestures]; } } [self _updateTextRangeByTrackingCaret]; From 91ab3108e3986e736988e466b6449903cb5f2698 Mon Sep 17 00:00:00 2001 From: Frank <472730949@qq.com> Date: Mon, 21 Feb 2022 14:03:30 +0800 Subject: [PATCH 2/2] Update YYTextView.h --- YYText/YYTextView.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/YYText/YYTextView.h b/YYText/YYTextView.h index 4893f806..cf6d9c40 100644 --- a/YYText/YYTextView.h +++ b/YYText/YYTextView.h @@ -406,6 +406,8 @@ IB_DESIGNABLE /// Only for YYTextView embedded in a `ScrollView`. Set this to block superview to respond PanGesture or scrolling action. @property (nullable, nonatomic, weak) UIScrollView *interactiveSuperScrollView; +@end + // Notifications, see UITextView's documentation for more information. UIKIT_EXTERN NSString *const YYTextViewTextDidBeginEditingNotification; UIKIT_EXTERN NSString *const YYTextViewTextDidChangeNotification;