iOS开发(5)动态监听键盘通知

      众所周知,在ios开发的页面传值和监听代理两个环节中,通知Notification是一个重量级角色。

这里主要介绍一下一种特殊ios自带的通知,如

UIKeyboardWillChangeFrameNotification


首先,让我们创建一个监听键盘的通知

//注册观察者

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyBoardWasChange:)name:UIKeyboardWillChangeFrameNotificationobject:nil];



这种通知是被苹果封装在UIWindow的.h文件中

UIKIT_EXTERNNSString *const UIKeyboardWillShowNotification;

UIKIT_EXTERNNSString *const UIKeyboardDidShowNotification; 

UIKIT_EXTERNNSString *const UIKeyboardWillHideNotification; 

UIKIT_EXTERNNSString *const UIKeyboardDidHideNotification;


UIKIT_EXTERNNSString *const UIKeyboardFrameBeginUserInfoKey       NS_AVAILABLE_IOS(3_2);// NSValue of CGRect

UIKIT_EXTERNNSString *const UIKeyboardFrameEndUserInfoKey         NS_AVAILABLE_IOS(3_2);// NSValue of CGRect

UIKIT_EXTERNNSString *const UIKeyboardAnimationDurationUserInfoKeyNS_AVAILABLE_IOS(3_0);// NSNumber of double

UIKIT_EXTERNNSString *const UIKeyboardAnimationCurveUserInfoKey   NS_AVAILABLE_IOS(3_0);// NSNumber of NSUInteger 


以上就是关于键盘所有的通知类型,但是基本只需要UIKeyboardWillChangeFrameNotification这一个就可以做到你想要的做的。

下面来实现注册通知时的相应方法

- (void)keyBoardWasChange:(NSNotification *)noti

{

   NSDictionary *infoDic = [noti userInfo];

    //由字典的键值对可知键盘的位置属性

    CGRect rect = [[infoDicobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

    CGFloat f = self.textfieldView.frame.origin.y + self.textfieldView.frame.size.height;

   CGFloat p = rect.origin.y;

    //弹出键盘

   if(p == 264)

    {

//下面是我自己封装的动画,就是怕键盘遮住了输入框而将输入框上移的动作

        [ZxkAnimationmoveAnimationWithView:self.textfieldViewfromPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y)ToPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 230)Duration:0.2];

    }

    //收回键盘

   else if(p ==480)

    {

        [ZxkAnimationmoveAnimationWithView:self.textfieldViewfromPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 230)ToPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y)Duration:0.2];

    }

    //弹出中文选择框

   else if(p ==228)

    {

//没记错的话,中文框应该是36高

        [ZxkAnimationmoveAnimationWithView:self.textfieldViewfromPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 230)ToPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 194)Duration:0.2];

    }

}


输出通知的infoDic可以看到下面几个键值对

UIKeyboardAnimationCurveUserInfoKey = 0;

    UIKeyboardAnimationDurationUserInfoKey = "0.25";

    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";

    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";

    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";

    UIKeyboardFrameChangedByUserInteraction = 0;

    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";


我刚才获取的就是UIKeyboardFrameEndUserInfoKey这个里面的值,是一个NSRect类型,主要是键盘的当前frame


最后,大家千万不要忘记在dealloc方法中移除通知

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

//由于我用的是ARC,所以不需要[super dealloc]

}


你可能感兴趣的:(iOS开发)