iOS开发UI篇----UI高级之键盘通知

/*

      1.监听键盘通知 UIKeyboardWillChangeFrameNotification

      2.实现监听通知的方法

      3.调整控制器View的位置

      4.当控制器销毁的时候移除通知

     */

//监听键盘通知的方法

/*

 UIKeyboardAnimationCurveUserInfoKey = 7;  键盘弹出时候,执行动画的节奏

 UIKeyboardAnimationDurationUserInfoKey = "0.25"; 键盘动画执行的时间

 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";  键盘大小

 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";  键盘开始的时候中心点位置

 UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";    键盘结束的时候中心点位置

 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 键盘开始时候的frame

 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";   键盘结束的时候的frame

 

//  1.监听键盘通知 UIKeyboardWillChangeFrameNotification

    

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

//2.实现监听通知的方法

- (void) keyboardWillChangeFrameNotification:(NSNotification *) notification

{

//    NSLog(@"%@",notification.userInfo);

    

// 键盘开始位置

    CGRect beginFrame  = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];

// 键盘结束的位置

    CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

//  取出控制器view的frame

    CGRect viewFrame = self.view.frame;

//  调整控制器view的y坐标

    viewFrame.origin.y += endFrame.origin.y - beginFrame.origin.y;

//  设置控制器的View的frame

    self.view.frame = viewFrame;

}

//移除监听者

-(void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

 

    

 

posted on 2015-10-20 22:27  DoNib  阅读(615)  评论(0编辑  收藏  举报

导航