UIImageView恢复原始帧

时间:2012-12-19 22:05:52

标签: ios uiviewanimation

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

1 个答案:

答案 0 :(得分:0)

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

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