首页 文章 精选 留言 我的

精选列表

搜索[虚拟线程],共10000篇文章
优秀的个人博客,低调大师

在iOS虚拟键盘上添加动态隐藏按钮

最近两周和团队做一个关于地理围栏技术的公交实时查询项目,为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘不同高度变换按钮位置,再不做输入的时候点击按钮能够隐藏键盘,这种方式在很多软件上都有体现,然后在网上查阅了关于检测键盘高度一些相关知识,以下是一个Demo,代码有很多需要优化地方,仅供需要者参考; 先看效果: 首先是我们在ViewDidLoada()中注册了两个通知,[NSNotificationCenterdefaultCenter],检测键盘动态,一个是键盘将要弹出的时候,另一个是键盘将要退出时候键盘的信息 - (void)viewDidLoad { NSLog(@"%@",NSStringFromSelector(_cmd)); [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } 检测键盘消息一个六种,根据字面意思差不多都能说明函数作用 UIKeyboardWillShowNotification 通知将要发布时候显示键盘 UIKeyboardDidShowNotification 通知发布后立即显示键盘 UIKeyboardWillHideNotification 通知发布前撤销键盘 UIKeyboardDidHideNotification 通知发布后撤销键盘 UIKeyboardWillChangeFrameNotification 通知发布前迅速变化的框架的键盘。 UIKeyboardDidChangeFrameNotification 通知发布后立即改变在键盘的框架。 NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制台显示打印出当前程序所调用的函数,我在下面每个函数都加了这一句,当我进行不同操作的时候,打印出被调用函数名,在调试程序时候比较适用吧; 注册消息通知后,实现通知所响应的方法 - (void)handleKeyboardDidShow:(NSNotification *)notification { NSLog(@"%@",NSStringFromSelector(_cmd)); NSDictionary *info = [notification userInfo]; CGRect keyboardFrame; [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size; CGFloat distanceToMove = kbSize.height; NSLog(@"---->动态键盘高度:%f",distanceToMove); if (exitButton == nil) { exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f); exitButton.frame = exitBtFrame; [exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal]; [self.view addSubview:exitButton]; } exitButton.hidden=NO; [self adjustPanelsWithKeyBordHeight:distanceToMove]; [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown]; } 在这个函数方法中值得探讨的是关于键盘所包含信息,因为每一次键盘弹出的时候也是动画形式弹出,他的坐标位置大小包含在userInfo的字典中,现在我用 NSLog(@"-->info:%@",info);打印出info对象,这些信息都可以在不同存储类型,取值的时候注意取值方式,此处只是提一提,希望以后有时间在做探讨, 在这一段代码上,后面注释了5行,因为打算当键盘推出的时候,按钮从视图上移除,或者释放按钮,但是都导致了应用程序崩溃,后来就没有释放和移除操作了 - (void)handleKeyboardWillHide:(NSNotification *)notification { NSLog(@"%@",NSStringFromSelector(_cmd)); if (exitButton.hidden==NO) { exitButton.hidden = YES; } // if (exitButton.superview) // { // [exitButton removeFromSuperview]; // [exitButton release]; // } } -(void)adjustPanelsWithKeyBordHeight:(float) height { NSLog(@"%@",NSStringFromSelector(_cmd)); if (exitButton) { CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f); exitButton.frame = exitBtFrame; [self.view addSubview:exitButton]; } // UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; // if (exitButton.superview == nil) // { // [tempWindow addSubview:exitButton]; // // 注意这里直接加到window上 // } } -(void)CancelBackKeyboard:(id)sender { NSLog(@"%@",NSStringFromSelector(_cmd)); [textField resignFirstResponder]; } - (void)viewDidUnload { [self setTextField:nil]; exitButton=nil; [super viewDidUnload]; // Release any retained subviews of the main view. } - (void)dealloc { [textField release]; [exitButton release]; [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所注册的通知 [super dealloc]; } 源代码:http://download.csdn.net/detail/duxinfeng2010/4831311 本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208706,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册