_dwebview 为webView 并引用IQKeyboardManager库
// 键盘弹起监听 添加通知
-(void)addEventListening
{
// 2.监听键盘的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
/**
* 当键盘改变了frame(位置和尺寸)的时候调用
*/
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
// 设置窗口的颜色
// self.window.backgroundColor = self.superview.backgroundColor;
// 0.取出键盘动画的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// CGRect keyboardFrameBeginRect = [[[note userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 1.取得键盘最后的frame UIKeyboardFrameEndUserInfoKey
CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 2.计算控制器的view需要平移的距离
CGFloat transformY = keyboardFrame.origin.y - kScreenHeight;
NSLog(@"高度 %lf ", transformY);
if (transformY < 0 && iPhoneX) {
transformY += 34;
}
// 3.执行动画
[self viewAnimateDuration:transformY Duration:duration view:self.view];
}
- (void)viewAnimateDuration:(CGFloat)transformY Duration:(CGFloat)duration view:(UIView *)view
{
CGRect rect = _dwebview.frame;
rect.origin.y = -transformY;
rect.size.height = kSCREEN_MAX + transformY;
_dwebview.frame = rect;
[UIView animateWithDuration:duration animations:^{
view.transform = CGAffineTransformMakeTranslation(0, transformY);
}];
}
网友评论