iOS 键盘自适应评论cell下方
2016-06-28 16:33
435 查看
首先判断点击评论按钮事件 和 回复评论事件(同时注册监听键盘出现和隐藏事件)
然后 获取到 所点击cell的坐标
然后 在键盘出来的时候计算所需要调整的高度
//注册键盘出现NSNotification
[[NSNotificationCenterdefaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotificationobject:nil];
//注册键盘隐藏NSNotification
[[NSNotificationCenterdefaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotificationobject:nil];
//cell的坐标
@property (nonatomic,assign)CGRect
rectSelected;
NSIndexPath * indexPath2 = [NSIndexPathindexPathForItem:index
inSection:0];
CGRect rectInTableView = [self.tableViewrectForRowAtIndexPath:indexPath2];
self.rectSelected = [self.tableViewconvertRect:rectInTableViewtoView:[self.tableViewsuperview]];//转化为在self.view
上的坐标
#pragma mark
#pragma mark keyboardWillShow
- (void)keyboardWillShow:(NSNotification *)notification
{
if (_KeySwitch) {
return;
}else{
_KeySwitch =YES;
}
//[0](null)
@"UIKeyboardFrameBeginUserInfoKey" : NSRect: {{0, 736}, {414, 293.66666666666674}}
// [1](null)
@"UIKeyboardCenterEndUserInfoKey" : NSPoint: {207, 589.16666666666663}
// [2](null)
@"UIKeyboardBoundsUserInfoKey" : NSRect: {{0, 0}, {414, 293.66666666666674}}
// [3](null)
@"UIKeyboardFrameEndUserInfoKey" : NSRect: {{0, 442.33333333333326}, {414, 293.66666666666674}}
// [5](null)
@"UIKeyboardCenterBeginUserInfoKey" : NSPoint: {207, 882.83333333333337}
// [4](null)
@"UIKeyboardAnimationDurationUserInfoKey" : (double)0.25
// [6](null)
@"UIKeyboardAnimationCurveUserInfoKey" : (long)7
// [7](null)
@"UIKeyboardIsLocalUserInfoKey" : (int)1
NSDictionary *userInfo = [notificationuserInfo];
NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];//aValueNSConcreteValue
* NSRect: {{0, 442.33333333333326}, {414, 293.66666666666674}}0x0000000136c0c4b0
__block CGFloat keyboardHeight = [aValueCGRectValue].size.height;//keyboardHeightCGFloat
293.66666666666674
CGRect keyboardRect = [aValueCGRectValue];//keyboardRectCGRect
(origin = (x = 0, y = 0), size = (width = 8, height = 0)) keyboardRect
CGRect(origin = (x = 0, y = 442.33333333333326), size = (width = 414, height = 293.66666666666674))
keyboardRect = [self.viewconvertRect:keyboardRectfromView:nil];////将rect从view中转换到当前视图中,返回在当前视图中的rect
CGFloat keyboardTop = keyboardRect.origin.y;//keyboardTopCGFloat
442.33333333333326
CGRect newTextViewFrame =self.view.bounds;//newTextViewFrameCGRect
(origin = (x = 0, y = 0), size = (width = 414, height = 442.33333333333326)) 键盘上面文字区域的坐标
newTextViewFrame.size.height = keyboardTop -self.view.bounds.origin.y;//上面文字的坐标 高度
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];//animationDurationNSTimeInterval
4.9406564584124654E-324
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//history_Y_offset btn转换成在self.view上的坐标
CGFloat delta =self.rectSelected.origin.y
+ self.rectSelected.size.height
- ([UIApplication
sharedApplication].keyWindow.bounds.size.height
- keyboardHeight-100);//deltaCGFloat
-54.333333333333258
-------------- 计算tableview要偏移的距离 ----------
CGPoint offset =self.tableView.contentOffset;//offsetCGPoint
(x = 3.4540270524485753E-314, y = 2.1232730692355269E-314) 表视图的偏移量
offset.y += delta;//offsetCGPoint
(x = 0, y = -118.33333333333326)
if (offset.y <0) {
offset.y = -64;//offsetCGPoint
(x = 0, y = -64)
}
[self.tableViewsetContentOffset:offset
animated:YES];
}
#pragma mark
#pragma mark keyboardWillHide
- (void)keyboardWillHide:(NSNotification *)notification {
NSValue *animationDurationValue = [notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIViewanimateWithDuration:animationDurationanimations:^{
// [self.chatKeyBoard keyboardDownForComment];
// self.chatKeyBoard.placeHolder = nil;
}];
}
然后 获取到 所点击cell的坐标
然后 在键盘出来的时候计算所需要调整的高度
//注册键盘出现NSNotification
[[NSNotificationCenterdefaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotificationobject:nil];
//注册键盘隐藏NSNotification
[[NSNotificationCenterdefaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotificationobject:nil];
//cell的坐标
@property (nonatomic,assign)CGRect
rectSelected;
NSIndexPath * indexPath2 = [NSIndexPathindexPathForItem:index
inSection:0];
CGRect rectInTableView = [self.tableViewrectForRowAtIndexPath:indexPath2];
self.rectSelected = [self.tableViewconvertRect:rectInTableViewtoView:[self.tableViewsuperview]];//转化为在self.view
上的坐标
#pragma mark
#pragma mark keyboardWillShow
- (void)keyboardWillShow:(NSNotification *)notification
{
if (_KeySwitch) {
return;
}else{
_KeySwitch =YES;
}
//[0](null)
@"UIKeyboardFrameBeginUserInfoKey" : NSRect: {{0, 736}, {414, 293.66666666666674}}
// [1](null)
@"UIKeyboardCenterEndUserInfoKey" : NSPoint: {207, 589.16666666666663}
// [2](null)
@"UIKeyboardBoundsUserInfoKey" : NSRect: {{0, 0}, {414, 293.66666666666674}}
// [3](null)
@"UIKeyboardFrameEndUserInfoKey" : NSRect: {{0, 442.33333333333326}, {414, 293.66666666666674}}
// [5](null)
@"UIKeyboardCenterBeginUserInfoKey" : NSPoint: {207, 882.83333333333337}
// [4](null)
@"UIKeyboardAnimationDurationUserInfoKey" : (double)0.25
// [6](null)
@"UIKeyboardAnimationCurveUserInfoKey" : (long)7
// [7](null)
@"UIKeyboardIsLocalUserInfoKey" : (int)1
NSDictionary *userInfo = [notificationuserInfo];
NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];//aValueNSConcreteValue
* NSRect: {{0, 442.33333333333326}, {414, 293.66666666666674}}0x0000000136c0c4b0
__block CGFloat keyboardHeight = [aValueCGRectValue].size.height;//keyboardHeightCGFloat
293.66666666666674
CGRect keyboardRect = [aValueCGRectValue];//keyboardRectCGRect
(origin = (x = 0, y = 0), size = (width = 8, height = 0)) keyboardRect
CGRect(origin = (x = 0, y = 442.33333333333326), size = (width = 414, height = 293.66666666666674))
keyboardRect = [self.viewconvertRect:keyboardRectfromView:nil];////将rect从view中转换到当前视图中,返回在当前视图中的rect
CGFloat keyboardTop = keyboardRect.origin.y;//keyboardTopCGFloat
442.33333333333326
CGRect newTextViewFrame =self.view.bounds;//newTextViewFrameCGRect
(origin = (x = 0, y = 0), size = (width = 414, height = 442.33333333333326)) 键盘上面文字区域的坐标
newTextViewFrame.size.height = keyboardTop -self.view.bounds.origin.y;//上面文字的坐标 高度
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];//animationDurationNSTimeInterval
4.9406564584124654E-324
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//history_Y_offset btn转换成在self.view上的坐标
CGFloat delta =self.rectSelected.origin.y
+ self.rectSelected.size.height
- ([UIApplication
sharedApplication].keyWindow.bounds.size.height
- keyboardHeight-100);//deltaCGFloat
-54.333333333333258
-------------- 计算tableview要偏移的距离 ----------
CGPoint offset =self.tableView.contentOffset;//offsetCGPoint
(x = 3.4540270524485753E-314, y = 2.1232730692355269E-314) 表视图的偏移量
offset.y += delta;//offsetCGPoint
(x = 0, y = -118.33333333333326)
if (offset.y <0) {
offset.y = -64;//offsetCGPoint
(x = 0, y = -64)
}
[self.tableViewsetContentOffset:offset
animated:YES];
}
#pragma mark
#pragma mark keyboardWillHide
- (void)keyboardWillHide:(NSNotification *)notification {
NSValue *animationDurationValue = [notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIViewanimateWithDuration:animationDurationanimations:^{
// [self.chatKeyBoard keyboardDownForComment];
// self.chatKeyBoard.placeHolder = nil;
}];
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法