iOS 键盘的弹出与收起

为什么80%的码农都做不了架构师?>>>   hot3.png

UIKeyboardWillShowNotification//弹出

UIKeyboardWillHideNotification//收起

 

所以需要监听这两个 最一些操作 比如 视图的移动

 

//注册观察键盘的变化[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didClickKeyboard:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didKboardDisappear:) name:UIKeyboardWillHideNotification object:nil];

方法实现

一般的:

#pragma mark - 键盘即将跳出
-(void)didClickKeyboard:(NSNotification *)sender{CGFloat durition = [sender.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];CGRect keyboardRect = [sender.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];CGFloat keyboardHeight = keyboardRect.size.height;[UIView animateWithDuration:durition animations:^{self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);}];
}
#pragma mark - 当键盘即将消失
-(void)didKboardDisappear:(NSNotification *)sender{CGFloat duration = [sender.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];[UIView animateWithDuration:duration animations:^{self.view.transform = CGAffineTransformIdentity;}];
}

 

如果视图在scrollview上边:

#pragma mark - 键盘即将跳出
-(void)didClickKeyboard:(NSNotification *)sender{[UIView animateWithDuration:0.1 animations:^{[self.scrollView setContentOffset:CGPointMake(0,30) animated:YES];}];}
#pragma mark - 当键盘即将消失
-(void)didKboardDisappear:(NSNotification *)sender{[UIView animateWithDuration:0.1 animations:^{[self.scrollView setContentOffset:CGPointMake(0,0) animated:YES];}];}

 

转载于:https://my.oschina.net/rainwz/blog/1941684


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部