监听键盘事件

通常通过以下方法监听键盘事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

- (void)keyboardChange:(NSNotification*)aNotification{
    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
    CGRect keyboardEndFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyboardY =  keyboardEndFrame.origin.y;
    
    CGFloat height = SCREEN_HEIGHT-keyboardY;
    BOOL show;
    if (ABS(height)>0.1) {
        //show
        show = YES;
    }else{
        //hide
        show = NO;
    }

你可能感兴趣的:(监听键盘事件)