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

简介:

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


效果图:

  

  



- (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]; } 

- (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];       }

- (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];      }

-(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];       }           }

-(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. }  
- (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





     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208699,如需转载请自行联系原作者



相关文章
|
14天前
|
安全 数据安全/隐私保护 iOS开发
基于iOS的动态权限管理实现
【4月更文挑战第9天】 随着移动互联网的快速发展,用户对应用程序的隐私安全要求越来越高。在iOS平台中,如何实现动态权限管理成为了开发者关注的焦点。本文将详细介绍一种基于iOS的动态权限管理实现方法,通过使用Core Motion框架和Notification Center,实现对用户位置信息的实时监控和动态权限申请。
|
7月前
|
iOS开发
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
28 0
|
20天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
7月前
|
Web App开发 安全 JavaScript
关于 Safari back 按钮在 iOS 16 不能按照期望工作的问题分析
关于 Safari back 按钮在 iOS 16 不能按照期望工作的问题分析
51 0
|
9月前
|
Android开发 iOS开发 Windows
无影产品动态|iOS & Android客户端6.0.0版本发布,提升触控灵敏度,操作体验更丝滑
无影ios & Android客户端6.0.0版本发布!移动端触控体验更舒适,用户操作更便捷,一起来看看!
676 0
无影产品动态|iOS & Android客户端6.0.0版本发布,提升触控灵敏度,操作体验更丝滑
|
9月前
|
存储 缓存 iOS开发
iOS 轻量化动态图像下载缓存框架实现
日常开发过程中,图片的下载会占用大量的带宽,图片的加载会消耗大量的性能和内存,正确的使用图片显得尤为重要。 同样也经常需要在各类型控件上读取网络图片和处理本地图片,例如:UIImageView、UIBtton、NSImageView、NSButton等等。
iOS 轻量化动态图像下载缓存框架实现
|
9月前
|
C语言 C++ iOS开发
iOS中C++静态全局变量的动态初始化时序
一个由于C++初始化失败导致Realm初始化失败的Crash
132 1
|
Web App开发 安全 JavaScript
关于 Safari back 按钮在 iOS 16 不能按照期望工作的问题分析
关于 Safari back 按钮在 iOS 16 不能按照期望工作的问题分析
118 0
|
监控 Android开发 iOS开发
Android6.0 源码修改之 仿IOS添加全屏可拖拽浮窗返回按钮
Android6.0 源码修改之 仿IOS添加全屏可拖拽浮窗返回按钮
104 0
|
API iOS开发 Perl
iOS Button连按处理防止按钮连续点击
iOS Button连按处理防止按钮连续点击