调用键盘的动画效果

来源:互联网 发布:设计班服要用什么软件 编辑:程序博客网 时间:2024/05/01 16:02


- (void)keyboardWillShown:(NSNotification *)aNotification {
 // 获得键盘大小
 NSDictionary *info = [aNotification userInfo];
 NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
 CGSize keyboardSize = [aValue CGRectValue].size;
 [UIView beginAnimations:nil context:NULL];
 // 设置动画
 [UIView setAnimationDuration:0.3];
 // 将toolBar的位置放到键盘上方
 CGRect frame = toolBar.frame;
 frame.origin.y -= keyboardSize.height;
 toolBar.frame = frame;
 //调整textView的高度
 frame = textView.frame;
 frame.size.height -= keyboardSize.height;
 textView.frame = frame;
 [UIView commitAnimations];
 NSRange endRange;
 endRange.location = [textView.text length];
 endRange.length = 1;
 // 将textView滚动到最后
 if(endRange.location > 0)
  [textView scrollRangeToVisible:endRange];
}

- (void)keyboardWillHidden:(NSNotification *)aNotification {
 NSDictionary *info = [aNotification userInfo];
 NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
 CGSize keyboardSize = [aValue CGRectValue].size;
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.3];
 CGRect frame = toolBar.frame;
 frame.origin.y += keyboardSize.height;
 toolBar.frame = frame;
 frame = textView.frame;
 frame.size.height += keyboardSize.height;
 textView.frame = frame;
 [UIView commitAnimations];
 NSRange endRange;
 endRange.location = [textView.text length];
 endRange.length = 1;
 if(endRange.location > 0)
  [textView scrollRangeToVisible:endRange];
}

原创粉丝点击