我的主页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];
}
答案 0 :(得分:0)
它可能与影响彼此的动画有关。这取决于您在动画发生时开始编辑的时间,添加和减去y
中的值是不可预测的。
在移动之前将原始帧保存在属性上,然后重置帧。当您处理几个重叠的动画时,这将产生更可预测的结果。