I have a custom text input view that adopts the UITextInput protocol. This view is embedded within a UIScrollView. The UITextinput adopting view includes UITextInteraction. When I drag the selection handles to the edges (top or bottom) of the current visible area, the view should scroll and select text automatically (like UITextView). Does anybody know how to achieve this?
I was hoping that UITextInteraction might inform me via its delegate of necessary events, but it does not (or even takes care of this functionality automatically).
I have tried to intercept closestPosition(to point: CGPoint) -> UITextPosition?, which is called whenever the user touches the UITextInput adopting view. Therefore, I can use it to track the dragging operation of a selection handle. Once, the user reaches the top of the view, I scroll up. However, I cannot detect when the user lets go of the handle (when touch ends). When this happens, the scrollView should stop scrolling. In my case, the scroll view keeps scrolling to the top.
I also tried to intercept selectionRects(for range: UITextRange) -> [UITextSelectionRect], but it is called sporadically during a scrolling.
I also cannot detect touchesEnded(). The UITextInteraction seems to block the call. Further, I cannot implement my own pan gesture. UITextInteraction blocks this as well during a selection operation.
Has anybody successfully used UITextInteraction? It seems very pre-mature at this stage.
Here's the way I got this info out of
UITextInteractionfor the text insertion point placement. I have not yet attempted to track the selection handles, but will update the answer if I manage itUITextInteractionhas a property calledgesturesForFailureRequirements. If you dump the content of these you'll notice one namedUIVariableDelayLoupeGesturewhich is a subclass ofUILongPressGestureRecognizer. This is the one we want when the use "picks up" the cursor with their fingerI add my own target/selector to this gesture recogniser when adding my text interaction like so:
Then in
longPressEdgeScrollGestureyou can get the coordinates of the cursor in your scroll view to activate your edge scrolling timer as necessary