iOS开发中键盘改变的监听


监听键盘

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

//执行的方法

-(void)keyboardWillChangeFrame:(NSNotification *)noti{

//1.获取动画时间

CGFloat time=[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

CGFloat endY=[noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;

//获取偏移值

//根据textField的位置调整偏移值

CGFloat offset=(endY-screenH)/2;

[UIView animateWithDuration:time/2 animations:^{

self.view.transform=CGAffineTransformMakeTranslation(0, offset);

}];

}

你可能感兴趣的:(iOS开发中键盘改变的监听)