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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

iOS开发(5)动态监听键盘通知

發布時間:2024/9/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发(5)动态监听键盘通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? 眾所周知,在ios開發的頁面傳值和監聽代理兩個環節中,通知Notification是一個重量級角色。

這里主要介紹一下一種特殊ios自帶的通知,如

UIKeyboardWillChangeFrameNotification


首先,讓我們創建一個監聽鍵盤的通知

//注冊觀察者

? ? [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyBoardWasChange:)name:UIKeyboardWillChangeFrameNotificationobject:nil];



這種通知是被蘋果封裝在UIWindow的.h文件中

UIKIT_EXTERNNSString *const UIKeyboardWillShowNotification;

UIKIT_EXTERNNSString *const UIKeyboardDidShowNotification;?

UIKIT_EXTERNNSString *const UIKeyboardWillHideNotification;?

UIKIT_EXTERNNSString *const UIKeyboardDidHideNotification;


UIKIT_EXTERNNSString *const UIKeyboardFrameBeginUserInfoKey? ? ? ?NS_AVAILABLE_IOS(3_2);// NSValue of CGRect

UIKIT_EXTERNNSString *const UIKeyboardFrameEndUserInfoKey? ? ? ? ?NS_AVAILABLE_IOS(3_2);// NSValue of CGRect

UIKIT_EXTERNNSString *const UIKeyboardAnimationDurationUserInfoKeyNS_AVAILABLE_IOS(3_0);// NSNumber of double

UIKIT_EXTERNNSString *const UIKeyboardAnimationCurveUserInfoKey? ?NS_AVAILABLE_IOS(3_0);// NSNumber of NSUInteger?


以上就是關于鍵盤所有的通知類型,但是基本只需要UIKeyboardWillChangeFrameNotification這一個就可以做到你想要的做的。

下面來實現注冊通知時的相應方法

- (void)keyBoardWasChange:(NSNotification *)noti

{

? ?NSDictionary *infoDic = [noti userInfo];

? ? //由字典的鍵值對可知鍵盤的位置屬性

? ? CGRect rect = [[infoDicobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

? ? CGFloat f = self.textfieldView.frame.origin.y + self.textfieldView.frame.size.height;

? ?CGFloat p = rect.origin.y;

? ? //彈出鍵盤

? ?if(p == 264)

? ? {

//下面是我自己封裝的動畫,就是怕鍵盤遮住了輸入框而將輸入框上移的動作

? ? ? ? [ZxkAnimationmoveAnimationWithView:self.textfieldViewfromPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y)ToPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 230)Duration:0.2];

? ? }

? ? //收回鍵盤

? ?else if(p ==480)

? ? {

? ? ? ? [ZxkAnimationmoveAnimationWithView:self.textfieldViewfromPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 230)ToPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y)Duration:0.2];

? ? }

? ? //彈出中文選擇框

? ?else if(p ==228)

? ? {

//沒記錯的話,中文框應該是36高

? ? ? ? [ZxkAnimationmoveAnimationWithView:self.textfieldViewfromPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 230)ToPoint:CGPointMake(self.textfieldView.center.x,self.textfieldView.center.y - f + 194)Duration:0.2];

? ? }

}


輸出通知的infoDic可以看到下面幾個鍵值對

UIKeyboardAnimationCurveUserInfoKey = 0;

? ? UIKeyboardAnimationDurationUserInfoKey = "0.25";

? ? UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

? ? UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";

? ? UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";

? ? UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";

? ? UIKeyboardFrameChangedByUserInteraction = 0;

? ? UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";


我剛才獲取的就是UIKeyboardFrameEndUserInfoKey這個里面的值,是一個NSRect類型,主要是鍵盤的當前frame


最后,大家千萬不要忘記在dealloc方法中移除通知

- (void)dealloc

{

? ? [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

//由于我用的是ARC,所以不需要[super dealloc]

}


與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的iOS开发(5)动态监听键盘通知的全部內容,希望文章能夠幫你解決所遇到的問題。

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