Listening for and Reacting to Keyboard Notifications(键盘通知)

  在输入文本时,键盘弹出,遮挡了视图内容,此时可通过键盘通知来使你的UI组件向上或向移动,或对它们进行重组。

  各键盘通知:

     UIKeyboardWillShowNotification:包含user-info dictionary

     UIKeyboardDidShowNotification

     UIKeyboardWillHideNotification:包含user-info dictionary

     UIKeyboardDidHideNotification

  user-info dictionary:

     UIKeyboardAnimationCurveUserInfoKey: 包含NSUInteger类型的NSNumber,封装在NSValue中

     UIKeyboardAnimationDurationUserInfoKey: 包含double类型的NSNumber,封装在NSValue中

     UIKeyboardFrameBeginUserInfoKey:CGRect类型封装在NSValue中,标识动画开始之前的keyboard的frame,使用前进行坐标转换

     UIKeyboardFrameEndUserInfoKey:CGRect类型封装在NSValue中,标识动画开始之后的keyboard的frame,使用前进行坐标转换


e.g.

@interface ViewController () <UITextFieldDelegate>

//View里包含ScrollView,ScrollView里包含ImageView,TextField

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITextField *textField; //此textField的delegate是自身ViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end


@implementation ViewController

//UITextFieldDelegte:return Button

- (BOOL) textFieldShouldReturn:(UITextField *)paramTextField{

    [paramTextField  resignFirstResponder];   

    return YES;

}

- (void) viewWillAppear:(BOOL)paramAnimated{

    [super viewWillAppear:paramAnimated];

    

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    //注册通知-键盘弹出

    [center addObserver:self selector:@selector(handleKeyboardWillShow:)

                   name:UIKeyboardWillShowNotification object:nil];

    //注册通知-键盘隐藏

    [center addObserver:self selector:@selector(handleKeyboardWillHide:)

                   name:UIKeyboardWillHideNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)paramAnimated{

    [super viewWillDisappear:paramAnimated];

    //取消注册

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{

    

    NSDictionary *userInfo = paramNotification.userInfo;

    

    //获取键盘弹出的动画持续时间,以便后续我们使用相同的时间进行动画呈现内容

    NSValue *animationDurationObject =  userInfo[UIKeyboardAnimationDurationUserInfoKey];

    //获取键盘弹出结束时的frame,是以屏幕坐标形式表示,因此后续使用需要转换成窗体坐标 convertRect:fromView:

    NSValue *keyboardEndRectObject = userInfo[UIKeyboardFrameEndUserInfoKey];

    

    double animationDuration = 0.0;

    CGRect keyboardEndRect = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);    

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    keyboardEndRect = [self.view convertRect:keyboardEndRect fromView:window];

    

    /* 键盘覆盖的部分*/

    CGRect intersectionOfKeyboardRectAndWindowRect =  CGRectIntersection(self.view.frame, keyboardEndRect);

    

    /* 滚动视图以显示遮挡内容*/

    [UIView animateWithDuration:animationDuration animations:^{      

        self.scrollView.contentInset = UIEdgeInsetsMake(0.0f,   //上

                                                        0.0f,   //左

                                                        intersectionOfKeyboardRectAndWindowRect.size.height, //下

                                                        0.0f);  //右

    //滚动视图使标识的rect刚好可见

    [self.scrollView  scrollRectToVisible:self.textField.frame animated:NO];

    }];   

}

- (void) handleKeyboardWillHide:(NSNotification *)paramSender{

    

    NSDictionary *userInfo = [paramSender userInfo];  

    NSValue *animationDurationObject =  [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

   

    double animationDuration = 0.0;   

    [animationDurationObject getValue:&animationDuration];

    

    [UIView animateWithDuration:animationDuration animations:^{

        self.scrollView.contentInset = UIEdgeInsetsZero; //0,0,0,0

    }];   

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值