【问题标题】:Keyboard hiding textfield animation for another View键盘隐藏另一个视图的文本字段动画
【发布时间】:2013-01-10 21:44:56
【问题描述】:

当我按下我的文本字段时,键盘会隐藏它。所以我已经实现了这段代码来“滚动”视图:

- (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 在另一个 nib 中。我认为由于我有两个 UIView,程序会变得混乱,不知道将动画设置到哪一个。

我没有收到任何错误,但动画没有发生。

我已经为我要制作动画的视图声明了 UIView“surveyPage”。上面的代码中是否有我必须指定要设置动画的 UIView 的地方?

更新:

下面的建议对我不起作用。我尝试将 self.view 更改为surveyPage。

在我上面发布的代码之上,我的程序中有这个:

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

上面的代码没有问题,但动画没有。

【问题讨论】:

标签: iphone ios xcode


【解决方案1】:

您在此处指定 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];

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 2011-08-23
    • 2021-05-07
    • 1970-01-01
    • 2016-07-24
    • 2014-03-18
    • 2011-01-19
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多