iOS开发中键盘改变的监听

2016-08-31  本文已影响0人  不疯魔难以成佛

<pre><code>
监听键盘

[[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);

}];

}

上一篇 下一篇

猜你喜欢

热点阅读