日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过NSNotification来监听键盘弹出和弹回

發布時間:2023/11/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过NSNotification来监听键盘弹出和弹回 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在通知中心建立一個廣播來監聽鍵盤的彈出和彈回,在監聽事件中加入觸發事件的一些操作。

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardwillhide:) name:UIKeyboardWillHideNotification object:nil];

監聽鍵盤的一些通知:

// 鍵盤的frame發生改變時發出的通知(位置和尺寸)// UIKeyboardWillChangeFrameNotification// UIKeyboardDidChangeFrameNotification// 鍵盤顯示時發出的通知// UIKeyboardWillShowNotification// UIKeyboardDidShowNotification// 鍵盤隱藏時發出的通知// UIKeyboardWillHideNotification// UIKeyboardDidHideNotification

在這里我需要實現的效果(如下圖)是在在鍵盤彈出時,使下方的toolbar向上移動到相應位置,因此需要知道鍵盤的高度和彈出動畫的時間,通過廣播監聽來得到鍵盤的frame和彈出動畫時間:

NSString *duration = userInfo[UIKeyboardAnimationDurationUserInfoKey];CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

再通過動畫效果,改變tableview和toolbar的frame,使得鍵盤在彈出時不會被遮擋:

[UIView animateWithDuration:[duration doubleValue] delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{_tableview.frame = CGRectMake(0, 64 , SIZE.width, SIZE.height - 64 - keyboardFrame.size.height - 50);footView.frame = CGRectMake(0, SIZE.height - keyboardFrame.size.height - 50, SIZE.width, 50);} completion:^(BOOL finished) {NSIndexPath *path = [NSIndexPath indexPathForRow:_dataArray.count - 1 inSection:0];[_tableview scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];}];

效果圖:

轉載于:https://www.cnblogs.com/moxuexiaotong/p/4928944.html

總結

以上是生活随笔為你收集整理的通过NSNotification来监听键盘弹出和弹回的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。