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

之前在 iOS虚拟键盘上添加动态隐藏按钮一文中描叙了关于键盘上添加动态按钮的操作,发现键盘上的按钮显示出来的时候很僵硬,此处做了改进,添加了动画过渡,更换了图片,能够让人感觉按钮是随着键盘的动画显示而显示,随着键盘的动画退出而退出,看上去更加流畅些;


效果图:

  

  



[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.     [super viewDidLoad];  
  5.     exitButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  6.     [exitButton setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];  
  7.     CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-48, self.view.frame.size.height , 48.0f, 30.0f);  
  8.       
  9.     [exitButton setFrame:exitBtFrame];  
  10.         
  11.      
  12.     [self.view addSubview:exitButton];  
  13.   
  14.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];    
  15.       
  16.      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];  
  17. }  

[cpp]  view plain copy
  1. - (void)handleKeyboardDidShow:(NSNotification *)notification   
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.     NSDictionary *info = [notification userInfo];  
  5.     NSLog(@"-->info:%@",info);  
  6.     CGRect keyboardFrame;  
  7.     [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];  
  8.     CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;  
  9.     NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];  
  10.     NSTimeInterval animationDuration;  
  11. //    copy value  
  12.     [animationDurValue getValue:&animationDuration];  
  13.       
  14. //    让键盘弹起的时候添加一个动画  
  15.     [UIView beginAnimations:@"animal" context:nil];  
  16.     [UIView setAnimationDuration:animationDuration];  
  17.       
  18.     CGFloat distanceToMove = kbSize.height;  
  19.     NSLog(@"---->动态键盘高度:%f",distanceToMove);  
  20.     [self adjustPanelsWithKeyBordHeight:distanceToMove];  
  21.     [UIView commitAnimations];  
  22.     exitButton.hidden=NO;  
  23.     [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown];  
  24.       
  25.   
  26. }  

[cpp]  view plain copy
  1. - (void)handleKeyboardWillHide:(NSNotification *)notification   
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.   
  5.     NSDictionary *info = [notification userInfo];  
  6.     CGRect keyboardFrame;  
  7.     [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];  
  8.     NSValue *animationDurValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];  
  9.     NSTimeInterval animationDuration;  
  10.     //   把animationDurvalue 值拷贝到animationDuration中  
  11.     [animationDurValue getValue:&animationDuration];  
  12.       
  13.     [UIView beginAnimations:@"animal" context:nil];  
  14.     [UIView setAnimationDuration:animationDuration];  
  15.   
  16.     if (exitButton) {  
  17.   
  18.         CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height, 48.0f, 30.0f);  
  19.         exitButton.frame = exitBtFrame;  
  20.         [self.view addSubview:exitButton];  
  21.   
  22.     }  
  23.     [UIView commitAnimations];  
  24.       
  25. }  

[cpp]  view plain copy
  1. -(void)adjustPanelsWithKeyBordHeight:(float) height  
  2. {  
  3.      
  4.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  5.     if (exitButton) {  
  6.   
  7.        CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 48, self.view.frame.size.height - height-30, 48.0f, 30.0f);  
  8.         exitButton.frame = exitBtFrame;  
  9.   
  10.         [self.view addSubview:exitButton];  
  11.   
  12.   
  13.     }  
  14.       
  15.       
  16. }  

[cpp]  view plain copy
  1. -(void)CancelBackKeyboard:(id)sender  
  2. {  
  3.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  4.       
  5.     [textField resignFirstResponder];  
  6.       
  7. }  
[cpp]  view plain copy
  1. - (void)viewDidUnload  
  2. {  
  3.     [self setTextField:nil];  
  4.     exitButton=nil;  
  5.     [super viewDidUnload];  
  6.       
  7.     // Release any retained subviews of the main view.  
  8. }  
[cpp]  view plain copy
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  4. }  
  5.   
  6. - (void)dealloc {  
  7.     [textField release];  
  8.     [exitButton release];  
  9.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  10.     [super dealloc];  
  11. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值