iOS虚拟键盘上添加动态按钮
2014-04-17 20:04
363 查看
之前在 在iOS虚拟键盘上添加动态隐藏按钮一文中描叙了关于键盘上添加动态按钮的操作,发现键盘上的按钮显示出来的时候很僵硬,此处做了改进,添加了动画过渡,更换了图片,能够让人感觉按钮是随着键盘的动画显示而显示,随着键盘的动画退出而退出,看上去更加流畅些;
效果图:
[cpp]
view plaincopy
- (void)viewDidLoad
{
NSLog(@"%@",NSStringFromSelector(_cmd));
[super viewDidLoad];
exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
[exitButton setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-48, self.view.frame.size.height , 48.0f, 30.0f);
[exitButton setFrame:exitBtFrame];
[self.view addSubview:exitButton];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
[cpp]
view plaincopy
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
NSLog(@"%@",NSStringFromSelector(_cmd));
NSDictionary *info = [notification userInfo];
NSLog(@"-->info:%@",info);
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
// copy value
[animationDurValue getValue:&animationDuration];
// 让键盘弹起的时候添加一个动画
[UIView beginAnimations:@"animal" context:nil];
[UIView setAnimationDuration:animationDuration];
CGFloat distanceToMove = kbSize.height;
NSLog(@"---->动态键盘高度:%f",distanceToMove);
[self adjustPanelsWithKeyBordHeight:distanceToMove];
[UIView commitAnimations];
exitButton.hidden=NO;
[exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];
}
[cpp]
view plaincopy
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
NSLog(@"%@",NSStringFromSelector(_cmd));
NSDictionary *info = [notification userInfo];
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
// 把animationDurvalue 值拷贝到animationDuration中
[animationDurValue getValue:&animationDuration];
[UIView beginAnimations:@"animal" context:nil];
[UIView setAnimationDuration:animationDuration];
if (exitButton) {
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height, 48.0f, 30.0f);
exitButton.frame = exitBtFrame;
[self.view addSubview:exitButton];
}
[UIView commitAnimations];
}
[cpp]
view plaincopy
-(void)adjustPanelsWithKeyBordHeight:(float) height
{
NSLog(@"%@",NSStringFromSelector(_cmd));
if (exitButton) {
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height - height-30, 48.0f, 30.0f);
exitButton.frame = exitBtFrame;
[self.view addSubview:exitButton];
}
}
[cpp]
view plaincopy
-(void)CancelBackKeyboard:(id)sender
{
NSLog(@"%@",NSStringFromSelector(_cmd));
[textField resignFirstResponder];
}
[cpp]
view plaincopy
- (void)viewDidUnload
{
[self setTextField:nil];
exitButton=nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
[cpp]
view plaincopy
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[textField release];
[exitButton release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
源码:http://download.csdn.net/detail/duxinfeng2010/4858444
效果图:
[cpp]
view plaincopy
- (void)viewDidLoad
{
NSLog(@"%@",NSStringFromSelector(_cmd));
[super viewDidLoad];
exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
[exitButton setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-48, self.view.frame.size.height , 48.0f, 30.0f);
[exitButton setFrame:exitBtFrame];
[self.view addSubview:exitButton];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
[cpp]
view plaincopy
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
NSLog(@"%@",NSStringFromSelector(_cmd));
NSDictionary *info = [notification userInfo];
NSLog(@"-->info:%@",info);
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
// copy value
[animationDurValue getValue:&animationDuration];
// 让键盘弹起的时候添加一个动画
[UIView beginAnimations:@"animal" context:nil];
[UIView setAnimationDuration:animationDuration];
CGFloat distanceToMove = kbSize.height;
NSLog(@"---->动态键盘高度:%f",distanceToMove);
[self adjustPanelsWithKeyBordHeight:distanceToMove];
[UIView commitAnimations];
exitButton.hidden=NO;
[exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];
}
[cpp]
view plaincopy
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
NSLog(@"%@",NSStringFromSelector(_cmd));
NSDictionary *info = [notification userInfo];
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
// 把animationDurvalue 值拷贝到animationDuration中
[animationDurValue getValue:&animationDuration];
[UIView beginAnimations:@"animal" context:nil];
[UIView setAnimationDuration:animationDuration];
if (exitButton) {
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height, 48.0f, 30.0f);
exitButton.frame = exitBtFrame;
[self.view addSubview:exitButton];
}
[UIView commitAnimations];
}
[cpp]
view plaincopy
-(void)adjustPanelsWithKeyBordHeight:(float) height
{
NSLog(@"%@",NSStringFromSelector(_cmd));
if (exitButton) {
CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height - height-30, 48.0f, 30.0f);
exitButton.frame = exitBtFrame;
[self.view addSubview:exitButton];
}
}
[cpp]
view plaincopy
-(void)CancelBackKeyboard:(id)sender
{
NSLog(@"%@",NSStringFromSelector(_cmd));
[textField resignFirstResponder];
}
[cpp]
view plaincopy
- (void)viewDidUnload
{
[self setTextField:nil];
exitButton=nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
[cpp]
view plaincopy
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[textField release];
[exitButton release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
源码:http://download.csdn.net/detail/duxinfeng2010/4858444
相关文章推荐
- 我的iPhone桌面
- Object-c Archiving归档
- 西蒙iphone-OpenGL ES教程英文原帖地址以及实例代码
- [开源项目]开心英语8000句 Iphone+Ipad版
- 月光微博客
- 月光微博客
- iPhone动画效果
- iPhone动画属性详解
- 获取crash report ,并解析定位代码
- 在iPhone项目中使用讯飞语音SDK实现语音识别和语音合成
- iPhone表情符号编码
- 深入浅出Cocoa之Bonjour网络编程(服务器端)
- iOS6全系列设备完美越狱工具发布
- 家庭文件共享:FreeNas服务器的搭建过程
- 如何设置iPhone无需密钥使用L2TP协议***
- HTML5 Loading来了
- [分享]iOS 5.0.1 桌面出现 四个白图标 的解决办法!AdSheet FieldTest iOS Diagnostics 设置。
- iphone Web App 导航设计探讨
- [转]从实现iPhone的OAuth封装看国内互联网和开放平台