Summary of code instances listening for keyboard events in iOS application development

  • 2020-05-19 05:56:50
  • OfStack

1. Register to listen for notifications of keyboard events


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];

2. Add animation to keyboard callbacks to appear and hide


- (void)keyboardWillShow:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
   
    CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat y = rect.origin.y;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    NSArray *subviews = [self subviews];
    for (UIView *sub in subviews) {
       
        CGFloat maxY = CGRectGetMaxY(sub.frame);
        if (maxY > y - 2) {
            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
        }
    }
    [UIView commitAnimations];
} - (void)keyboardShow:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
} - (void)keyboardWillHide:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    NSArray *subviews = [self subviews];
    for (UIView *sub in subviews) {
        if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
        }
    }
    [UIView commitAnimations];
} - (void)keyboardHide:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
}

3. Monitor the non-proxy implementation of the keyboard delete key
In UITextField and UITextView, how to listen for the delete key.

I see the Internet is using proxy monitoring, I feel unreliable.

Apple has made that clear.

It's in the protocol that they implemented


NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView<UITextInput>
@protocol UITextInput<UIKeyInput>
@protocol UIKeyInput <UITextInputTraits> - (BOOL)hasText; - (void)insertText:(NSString *)text; - (void)deleteBackward; @end

It's written very clearly, so you can see the 1.

-deleteBackward is the delete button listener.

You can override this method by subclassing it yourself.


Related articles: