标签:
- (void)keyboardWillChange:(NSNotification *)notification
{
/*
userInfo = {
// 键盘弹出的节奏
UIKeyboardAnimationCurveUserInfoKey = 7;
//键盘弹出执行动画的时间
UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
// 键盘弹出时候的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
// 键盘隐藏时候的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
}}
*/
NSLog(@"键盘弹出 %@", notification);
/*
计算需要移动的距离
弹出的时候移动的值 = 键盘的Y值 – 控制view的高度 = 要移动的距离
- 480 = -216
隐藏的时候移动的值 = 键盘的Y值 - 控制view的高度 = 要移动的距离
480 - 480 = 0
*/
// 1.获取键盘的Y值
NSDictionary *dict = notification.userInfo;
CGRect keyboardFrame = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardY = keyboardFrame.origin.y;
// 获取动画执行时间
CGFloat duration = [dict[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
// 2.计算需要移动的距离
CGFloat translationY = keyboardY - self.view.frame.size.height;
// 通过动画移动view
/*
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, translationY);
}];
*/
/*
输入框和键盘之间会由一条黑色的线条, 产生线条的原因是键盘弹出时执行动画的节奏和我们让控制器view移动的动画的节奏不一致导致
*/
[UIView animateWithDuration:duration delay:0.0 options:7 << 16 animations:^{
// 需要执行动画的代码
self.view.transform = CGAffineTransformMakeTranslation(0, translationY);
} completion:^(BOOL finished) {
// 动画执行完毕执行的代码
}];
}
键盘弹出的现象
标签:
原文地址:http://blog.csdn.net/guoyule2010/article/details/44009749