13

現在、キーボードの上に UITextField があります。タップすると、キーボードの上にくっついてスムーズに上に移動するはずです。キーボードの正確な持続時間とアニメーションの種類がわからないので、非常にでこぼこです。ここに私が持っているものがあります:

[theTextView resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
// Frame changes go here (move down 216px)
[UIView commitAnimations];

[theTextView becomeFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
// Frame changes go here (move up 216px)
[UIView commitAnimations];

誰かが以前にこのようなことをしたことがある場合は、アニメーションをスムーズにし、バーがキーボードの上部に「引っかかっている」ように見せるために使用した設定を知りたい.

4

4 に答える 4

46

UIKitUIKeyboardWillShowNotificationは、キーボードをUIKeyboardWillHideNotification表示したときと、キーボードを非表示にしたときに投稿します。これらの通知には、を適切にアニメーション化するために必要なすべてのものが含まれていますUITextField

UITextFieldあなたがと呼ばれるプロパティにいるとしましょうmyTextField

まず、どこかで通知を登録する必要があります。登録する場所は、移動の原因となるオブジェクトによって異なりますmyTextField。私のプロジェクトでは、フィールドのスーパービューが責任を負い、ペン先からUIをロードするので、スーパービューでそれを行いますawakeFromNib

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
}

を使用しUIViewControllerてフィールドを移動する場合は、で実行することをお勧めしますviewWillAppear:animated:

deallocまたはviewWillDisappear:animated::で登録を解除する必要があります

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

もちろん、トリッキーなビットはkeyboardWillHideOrShow:メソッドにあります。まず、通知からアニメーションパラメータを抽出します。

- (void)keyboardWillHideOrShow:(NSNotification *)note
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

keyboardFrameグローバル座標系にあります。myTextField.frameフレームをと同じ座標系に変換する必要がありmyTextField.frame、次の座標系にありmyTextField.superviewます:

    CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];

次に、移動したいフレームを計算myTextFieldします。新しいフレームの下端は、キーボードのフレームの上端と同じである必要があります。

    CGRect newTextFieldFrame = self.myTextField.frame;
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;

最後にmyTextField、キーボードが使用しているのと同じアニメーションパラメータを使用して、新しいフレームにアニメーション化します。

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.myTextField.frame = newTextFieldFrame;
    } completion:nil];
}

ここにすべてがまとめられています:

- (void)keyboardWillHideOrShow:(NSNotification *)note
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];

    CGRect newTextFieldFrame = self.myTextField.frame;
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.myTextField.frame = newTextFieldFrame;
    } completion:nil];
}
于 2012-01-02T19:04:18.660 に答える
5

テキスト フィールド (またはテキスト フィールドを保持するビュー) を、inputAccessoryView編集中のフィールドの として設定します。その後、キーボードの上部に自動的に取り付けられ、適切にアニメーション化されます。

于 2012-01-02T19:11:48.310 に答える
0

UITextField を (アニメーションで) キーボードにドッキングするようにするには、メソッドを使用してオフセット計算を行い、スクロール ビューにオフセット変更を適用する必要があります (UITextField が UIScrollView に配置されていると仮定します) setContentOffset:animation:

于 2012-01-02T18:42:21.833 に答える
0

キーボード通知で次のコードを使用します。

@objc func keyboardWillShowNotification(notification: Notification) {
    let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height ?? 216
    let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0.25
    let curve = (notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? UInt ?? 7) << 16
    tableViewBottomConstraint.constant = keyboardHeight - submitButtonContainer.frame.size.height

    UIView.animate(withDuration: duration, delay: 0, options: [UIViewAnimationOptions(rawValue: curve)], animations: {
        // Animation
    }, completion: nil)
}
于 2018-07-19T05:29:11.613 に答える