//通知中心addObserver
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(transformView:) name:UIKeyboardWillChangeFrameNotification object:nil];
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for (UIView *view in self.view.subviews) {
[view resignFirstResponder];
}
}
//上移UIView
-(void)transformView:(NSNotification *)aNSNotification{
//获取键盘弹出前的Rect
NSValue *keyBoardBeginBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect beginRect=[keyBoardBeginBounds CGRectValue];
//获取键盘弹出后的Rect
NSValue *keyBoardEndBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect endRect=[keyBoardEndBounds CGRectValue];
//获取键盘位置变化前后纵坐标Y的变化值
CGFloat deltaY=endRect.origin.y-beginRect.origin.y;
//在0.25秒内完成self.view的Fram变化,等于是给self.view添加一个向上移动 deltaY 的动画
[UIView animateWithDuration:0.25f animations:^{
[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+deltaY, self.view.frame.size.width , self.view.frame.size.height)];
}];
}