
在IOS开发过程中,监听键盘d出事件,修改对应的UI改变,会使用户体验更加丰富。
首先直接看代码
// 监听键盘d出事件,控制toolbar位置 NSNotificationCenter.defaultCenter().addobserver(self,selector: "onKeyboarDWillChangeFrame:",name: UIKeyboarDWillChangeFrameNotification,object: nil);
/** 键盘显示隐藏事件监听 */ func onKeyboarDWillChangeFrame(notification: NSNotification) { // 1、将通知中的数据转换成NSDictionary let dict = NSDictionary(dictionary: notification.userInfo!); // 2、获取键盘最后的Frame值 let keyboardFrame = dict[UIKeyboardFrameEndUserInfoKey]!.CGRectValue(); // 3、获取键盘移动值 let ty = keyboardFrame.origin.y - vIEw.frame.height; // 4、获取键盘d出动画事件 let duration = dict[UIKeyboardAnimationDurationUserInfoKey] as Double; UIVIEw.animateWithDuration(duration,animations: { () -> VoID in// 5、设置整个屏幕随键盘移动 self.toolbar.transform = CGAffinetransformMakeTranslation(0,ty); });// 键盘d出隐藏所执行的 *** 作数据// UIKeyboardAnimationCurveUserInfoKey = 7;// UIKeyboardAnimationDurationUserInfoKey = "0.25"; 键盘d出/隐藏时动画时间// UIKeyboardBoundsUserInfoKey = "NSRect: {{0,0},{375,258}}";// UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5,796}";// UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5,538}";// UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0,667},258}}";// UIKeyboardFrameEndUserInfoKey = "NSRect: {{0,409},258}}"; } deinit { // 控制器销毁时,移除消息通知监听(必须) NSNotificationCenter.defaultCenter().removeObserver(self); } 总结 以上是内存溢出为你收集整理的Swift之键盘事件全部内容,希望文章能够帮你解决Swift之键盘事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)