2013-01-10 121 views
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的,我想这样做的是另外一个笔头。我想因为我有两个UIViews,程序越来越困惑,并且不知道将哪个设置为动画。

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

我已经宣布UIView“surveyPage”为我试图动画的视图。在上面的代码中有什么地方需要指定我想要动画的UIView?

更新:

的建议之下并没有为我工作。我试着将self.view更改为surveyPage。

在上面,我上面贴的代码,我有这个在我的程序:

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

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

+0

我用类似的工作了一段时间后,也许一些代码,这里将帮助http://stackoverflow.com/questions/11722732/keyboard-hiding-uiview-ios –

+0

感谢您的链接,但它没有帮助:( – Adam

+0

@亚当:嗨..你解决了这个问题吗?我也有同样的问题,除了我的文本框是在子视图内,而不是主视图 –

回答

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]; 
+0

这并不适用于我,请参阅我的更新 – Adam