0

テキストフィールドを押すと、キーボードが非表示にします。したがって、ビューを上に「スクロール」するためにこのコードを実装しました。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 80; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

これを実行しようとしている UIView は別のペン先にあります。2 つの UIView があるため、プログラムが混乱し、アニメーションをどちらに設定すればよいかわかりません。

エラーは発生しませんが、アニメーションは発生していません。

アニメーション化しようとしているビューの UIView "surveyPage" を宣言しました。上記のコードのどこかに、アニメーション化したい UIView を指定する必要がありますか?

アップデート:

以下の提案は私にはうまくいきませんでした。self.view を surveyPage に変更してみました。

上に投稿したコードの上に、プログラムにこれがあります。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

上記のコードは問題なく動作しますが、アニメーションは動作しません。

4

1 に答える 1

0

ここで UIView を指定しています。

self.view.frame = CGRectOffset(self.view.frame, 0, movement);

ですself.view

試す:

[UIView animateWithDuration:movementDuration animations:^{
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}];

それ以外の

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
于 2013-01-10T21:49:34.550 に答える