我的主页viewController
打开UIImageView
一个标志。viewDidAppear
然后运行两个动画。徽标向上的翻译,以及UITextField
. 当用户单击UITextField textFieldDidBeginEditing
运行并将视图的框架设置为动画时(以允许用户查看UITextField
键盘将覆盖的内容)。当用户单击键盘textFieldDidEndEditing
运行的任何位置并动画视图的框架时。运行后textFieldDidEndEditing
,我UIImageView
又回到了原来的位置(在动画开始之前)。为什么会发生这种情况,我应该如何解决?谢谢。
static const CGFloat STARTPAGE_ANIMATION_TIME = 0.5;
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear:");
CGRect logoFrame = logoImage.frame;
logoFrame.origin.y = 130;
[UIImageView animateWithDuration:STARTPAGE_ANIMATION_TIME delay:STARTPAGE_ANIMATION_TIME options:UIViewAnimationCurveEaseInOut animations:^{
logoImage.frame = logoFrame;
} completion:^(BOOL finished) {
NSLog(@"logoImageAnimation");
}];
[UIImageView animateWithDuration:STARTPAGE_ANIMATION_TIME delay:STARTPAGE_ANIMATION_TIME options:UIViewAnimationCurveEaseInOut animations:^{
searchField.alpha = 1;
} completion:^(BOOL finished) {
NSLog(@"searchFieldAnimation");
}];
}
CGFloat ANIMATED_DISTANCE = 200;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing:");
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= ANIMATED_DISTANCE;
[UIView animateWithDuration:KEYBOARD_ANIMATION_DURATION animations:^{
self.view.frame = viewFrame;
} completion:^(BOOL finished) {
NSLog(@"frameAnimationUp");
}];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing:");
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += ANIMATED_DISTANCE;
[UIView animateWithDuration:KEYBOARD_ANIMATION_DURATION animations:^{
self.view.frame = viewFrame;
} completion:^(BOOL finished) {
NSLog(@"frameAnimationDown");
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}