Introduce a,

IOS14 iPad adds the function of writing at will for Pencil, which can write with a stylus in a specified area without popping up the keyboard.

Second, basic class

There are two main classes of write on the go

1. UIScribbleInteraction (can be realized in the textField, textView, etc.) 2. UIIndirectScribbleInteraction (can be in any View, but there must be corresponding to the first person to receive)Copy the code

1. UIIndirectScribbleInteraction

To implement authoring, you need to implement a proxy method (like a UITableViewDelegate)

Copy the code

Its proxy methods are as follows:

/// specify which field can be used with arbitrary writing, and set a unique ID. - (void)indirectScribbleInteraction:(UIIndirectScribbleInteraction *)interaction requestElementsInRect:(CGRect)rect Completion: (void (^) (NSArray < UIScribbleElementIdentifier > * elements)) completion / / determine the range of writing - (CGRect)indirectScribbleInteraction:(UIIndirectScribbleInteraction *)interaction FrameForElement (UIScribbleElementIdentifier) elementIdentifier; / / on what object - (void) indirectScribbleInteraction: (UIIndirectScribbleInteraction *) interaction focusElementIfNeeded:(UIScribbleElementIdentifier)elementIdentifier referencePoint:(CGPoint)focusReferencePoint Completion :(void (^)(UIResponder<UITextInput> * _Nullable))completion // determine whether a particular element is focused - (BOOL)indirectScribbleInteraction:(UIIndirectScribbleInteraction *)interaction IsElementFocused: UIScribbleElementIdentifier elementIdentifier / / / whether focus - delay (BOOL)indirectScribbleInteraction:(UIIndirectScribbleInteraction *)interaction ShouldDelayFocusForElement: UIScribbleElementIdentifier elementIdentifier if there is a handwriting to write - / / / (void)indirectScribbleInteraction:(UIIndirectScribbleInteraction *)interaction WillBeginWritingInElement: UIScribbleElementIdentifier elementIdentifier / / / finish - whether writing (void)indirectScribbleInteraction:(UIIndirectScribbleInteraction *)interaction didFinishWritingInElement:(UIScribbleElementIdentifier)elementIdentifierCopy the code

Three, basic use

1. Declare objects

@property (nonatomic, strong) UIIndirectScribbleInteraction *interaction;
Copy the code

2. Create an Interaction object to sign the proxy and add Interaction to the current View (in this case keyboradInputView is UIView)

self.interaction = [[UIIndirectScribbleInteraction alloc] initWithDelegate:self];
[self.keyboradInputView addInteraction:self.interaction];
Copy the code


/ / / writable area - (void) indirectScribbleInteraction (UIIndirectScribbleInteraction *) interaction requestElementsInRect:(CGRect)rect completion:(void (^)(NSArray<UIScribbleElementIdentifier> * _Nonnull))completion API_AVAILABLE(ios(14.0)){// Provide an identifier (similar to UITableView's Indentifire, but unique here, Completion (@[@" KeyboradInputView.c_textinputVieWindetiner "]); }Copy the code
/ / determine the range of writing - (CGRect) indirectScribbleInteraction (UIIndirectScribbleInteraction *) interaction FrameForElement: UIScribbleElementIdentifier elementIdentifier API_AVAILABLE (ios (14.0)) {return [UIScreen mainScreen].bounds; }Copy the code
/ / the focus to the current object - (void) indirectScribbleInteraction (UIIndirectScribbleInteraction *) interaction focusElementIfNeeded:(UIScribbleElementIdentifier)elementIdentifier referencePoint:(CGPoint)focusReferencePoint Completion :(void (^)(UIResponder<UITextInput> * _Nullable))completion API_AVAILABLE(ios(14.0)){ [self.keyboradInputView.c_textInputView becomeFirstResponder]; completion(self.keyboradInputView.c_textInputView); }Copy the code
/ / identify a particular element has been the focus - (BOOL) indirectScribbleInteraction (UIIndirectScribbleInteraction *) interaction IsElementFocused: UIScribbleElementIdentifier elementIdentifier API_AVAILABLE (ios (14.0)) {return self.keyboradInputView.c_textInputView.isFirstResponder; }Copy the code

Four, UIScribbleInteraction

  • Is similar to UIIndirectScribbleInteraction use way, but UIScribbleInteraction can only identify textField, textView but much more

Fifth, pay attention to

It’s worth noting that iOS14, which is a write function, does not play the virtual keyboard, and does not receive the virtual keyboard pop-up notification: the following notification is invalid

Invalid UIKeyboardWillShowNotification UIKeyboardWillHideNotification is invalidCopy the code
  • Write on the Go currently does not receive notifications like the one above.
  • Currently, it is not clear whether the user has enabled or disabled the write function (it can only be determined by whether the virtual keyboard pops up).

Sixth, other

  • This article only gives ideas, in fact, the internal also a few attributes and API interested friends can go to have a look. There are no blogs in China or abroad to refer to. If you look at the official documentation, it’s not hard