欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IOS键盘监听

程序员文章站 2024-01-29 08:50:52
...

   键盘消失

 [[NSNotificationCenterdefaultCenteraddObserver:selfselector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotificationobject:nil];

   键盘出现

 [[NSNotificationCenterdefaultCenteraddObserver:selfselector:@selector(keyboardWasShow:) name:UIKeyboardDidShowNotification object:nil];

所有key值

UIKIT_EXTERNNSString *const UIKeyboardWillShowNotification;

UIKIT_EXTERNNSString *const UIKeyboardDidShowNotification; 

UIKIT_EXTERNNSString *const UIKeyboardWillHideNotification; 

UIKIT_EXTERNNSString *const UIKeyboardDidHideNotification;

触发方法

- (void)keyboardWillShow:(NSNotification *)notification 

{  

    CGPoint beginCentre = [[[notification userInfo] valueForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue]; 

    CGPoint endCentre = [[[notification userInfo] valueForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue]; 

    CGRect keyboardBounds = [[[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey] CGRectValue]; 

    CGRect keyboardFrames = [[[notification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

    UIViewAnimationCurve animationCurve = [[[notification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; 

    NSTimeInterval animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

**若有警告可将key值写为@""中