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)动态监听键盘通知的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS开发(4)UITextField
- 下一篇: ios开发(6)uiimageView