0

我的主页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];
}
4

1 回答 1

0

它可能与相互影响的动画有关。如果您在动画发生时开始编辑,这取决于时间,添加和减去一个值y是不可预测的。

在移动之前将原始帧保存在属性上,然后重置帧。当您处理多个重叠动画时,这将产生更可预测的结果。

于 2012-12-19T22:36:29.663 回答