您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 鍵盤彈出通知與自訂動畫

編輯:iOS 相關資訊

鍵盤彈出或收起時,可透過 NSNotificationCenter 監聽 UIKeyboardWillShowNotification 與 UIKeyboardWillHideNotification,並在通知中取得鍵盤尺寸、動畫時間與曲線。

- (void)keyboardWillShow:(NSNotification *)note {
    CGRect keyboardBounds = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
    CGRect frame = self.editView.frame;
    frame.origin.y = self.view.bounds.size.height - keyboardBounds.size.height - frame.size.height;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    self.editView.frame = frame;
    [UIView commitAnimations];
}

收起鍵盤時,可將輸入框移回畫面下方,並使用相同的動畫參數,讓視覺體驗更一致。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved