2013-08-20 76 views
1

當鍵盤動畫顯示/完全隱藏時,有沒有什麼代碼/方法可以捕捉?我需要在keyboardDidShow通知被調用之前鍵盤半可見時執行一些操作。 任何幫助真的很感激。 在此先感謝。UIKeyboard隱藏/顯示方法?

回答

0

這裏沒有內置的方法。但是,正如@Aaron Brager指出的那樣,您可以使用UIKeyboardWillShowNotification通知的用戶信息中的UIKeyboardAnimationDurationUserInfoKey來獲取鍵盤演示動畫的持續時間。那麼你所要做的就是將這個值除以2,並延遲執行你的動作的數量。這裏有一個例子:

- (void)keyBoardWillShow:(NSNotification *)notification 
{ 
    NSTimeInterval duration = [self keyboardAnimationDurationForNotification:notification]; 

    double delayInSeconds = duration/2.0; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 

    }); 
} 

- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification 
{ 
    NSDictionary *info = [notification userInfo]; 
    NSValue *value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval duration = 0; 
    [value getValue:&duration]; 
    return duration; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 

    [super viewWillAppear:animated]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
} 
+0

我想你可以從UIKeyboardWillShowNotification的userinfo字典中減半動畫持續時間吧? –

+0

@AaronBrager更新。感謝您指出了這一點! –

+0

它幫助。感謝0x7fffffff和Aaron。 – Raj