Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YYTextView selection bug that selection cannot be done when selecting one more line (trigger scrolling action) #953

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions YYText/YYTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ 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;

@end

// Notifications, see UITextView's documentation for more information.
UIKIT_EXTERN NSString *const YYTextViewTextDidBeginEditingNotification;
Expand Down
24 changes: 16 additions & 8 deletions YYText/YYTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -2500,6 +2507,7 @@ - (CGSize)sizeThatFits:(CGSize)size {
return layout.textBoundingSize;
}


#pragma mark - Override UIResponder

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
Expand Down Expand Up @@ -2534,15 +2542,15 @@ - (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];
} else {
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];
}
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand Down