iOS 监听键盘实现页面上滑下滑方法
2016-02-25 14:09
453 查看
键盘弹起下落连带页面滑动效果大同小异.
好吧.几句话.
首先,在viewdidload里对键盘进行监听.
//监听键盘
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
监听方法是自己写的,不习惯的可以自行修改
//监听键盘升起的方法
- (void)keyBoardWillShow:(NSNotification *)n {
//NSLog(@"%@",n.userInfo);
//获得键盘升起的时间
NSTimeInterval time = [[n.userInfo
objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"]
floatValue];
//获得键盘升起的高度
float height = [[n.userInfo
objectForKey:@"UIKeyboardFrameEndUserInfoKey"]
CGRectValue].size.height;
[UIView
animateWithDuration:time animations:^{
CGRect frame =
self.view.frame;
frame.origin.y = -height;
self.view.frame = frame;
[self.view
layoutIfNeeded];
}];
}
//监听键盘隐藏的方法
- (void)keyBoardWillHide:(NSNotification *)n {
NSTimeInterval time = [[n.userInfo
objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"]
floatValue];
[UIView
animateWithDuration:time animations:^{
CGRect frame =
self.view.frame;
frame.origin.y =
0;
self.view.frame = frame;
[self.view
layoutIfNeeded];
}];
}
//最后打个广告,新建iOS技术群,欢迎大家加入进行技术讨论.群号:207577704
好吧.几句话.
首先,在viewdidload里对键盘进行监听.
//监听键盘
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
监听方法是自己写的,不习惯的可以自行修改
//监听键盘升起的方法
- (void)keyBoardWillShow:(NSNotification *)n {
//NSLog(@"%@",n.userInfo);
//获得键盘升起的时间
NSTimeInterval time = [[n.userInfo
objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"]
floatValue];
//获得键盘升起的高度
float height = [[n.userInfo
objectForKey:@"UIKeyboardFrameEndUserInfoKey"]
CGRectValue].size.height;
[UIView
animateWithDuration:time animations:^{
CGRect frame =
self.view.frame;
frame.origin.y = -height;
self.view.frame = frame;
[self.view
layoutIfNeeded];
}];
}
//监听键盘隐藏的方法
- (void)keyBoardWillHide:(NSNotification *)n {
NSTimeInterval time = [[n.userInfo
objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"]
floatValue];
[UIView
animateWithDuration:time animations:^{
CGRect frame =
self.view.frame;
frame.origin.y =
0;
self.view.frame = frame;
[self.view
layoutIfNeeded];
}];
}
//最后打个广告,新建iOS技术群,欢迎大家加入进行技术讨论.群号:207577704
相关文章推荐
- iOS之自定义pickerview(行驶里程数)
- iOS开发那些事--性能优化–内存泄露问题的解决
- Nagios 监控系统架设全攻略
- iOS 25个性能优化/内存优化常用方法
- iOS中跳转控制器隐藏底部tabBar
- iOS项目内存优化
- iOS原生API生成二维码(CIFilter)
- kidd风的IOS日志之摄像头以及相册的调用
- ios 关于如何获取iphone或iPad的ip地址
- iOS的 tableView的自适应高度, XML 的 DOM 解析
- iOS9 & Xcode7 下设置LaunchImage启动图片 问题及解决
- iOS 电话在后台运行时,我的启动图片被压缩
- kidd风的IOS日志之IOS9 视频播放控制器(AVPlayer)
- 关于苹果开发证书失效的解决方案(2016年2月14日Failed to locate or generate matching signing assets)
- iOS开发 更改顶部状态栏颜色
- iOS如果获取蓝牙Mac地址
- iOS开发 获取图片点击位置的颜色
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
- ios NSNotificationCenter消息通讯机制
- iOS通过代码关闭程序