-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
//在這里我多加了62,(加上了輸入中文選擇文字的view高度)這個依據自己需求而定
int offset = (frame.origin.y + 20 * widthPropor)- ( self.view.frame.size.height - 216.0);//鍵盤高度216
//設置動畫的名字
[UIView beginAnimations:@"Animation" context:nil];
//設置動畫的間隔時間
[UIView setAnimationDuration:0.42];
//使用當前正在運行的狀態開始下一段動畫
[UIView setAnimationBeginsFromCurrentState: YES];
if (offset>0) {
//將視圖的Y坐標向上移動offset個單位,以使下面騰出地方用于軟鍵盤的顯示
self.view.frame = CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);
}
//設置動畫結束
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
//設置動畫的名字
[UIView beginAnimations:@"Animation" context:nil];
//設置動畫的間隔時間
[UIView setAnimationDuration:0.42];
//??使用當前正在運行的狀態開始下一段動畫
[UIView setAnimationBeginsFromCurrentState: YES];
//設置視圖移動的位移
self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
//設置動畫結束
[UIView commitAnimations];
}