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]

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值