ViewController加载视图的动画

时间:2014-11-06 20:00:07

标签: ios objective-c xcode

当我的一个ViewControllers加载时,我正在执行动画。我在viewDidAppear方法中调用它。它看起来像这样。

[UIView animateKeyframesWithDuration:2.5f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:1.0f animations:^{
            CGRect rocketFrame = _rocketImageView.frame;
            rocketFrame.origin.y = rocketFinishPosition;
            _rocketImageView.frame = rocketFrame;

            CGRect flameFrame = _flameImageView.frame;
            flameFrame.origin.y = rocketFinishPosition + rocketFrame.size.height - 3;
            _flameImageView.frame = flameFrame;
            _flameImageView.hidden = false;

            CGRect earthFrame = _earthImageView.frame;
            earthFrame.origin.y = earthFinishPosition;
            _earthImageView.frame = earthFrame;

                CGRect moonFrame = _moonImageView.frame;
                moonFrame.origin.y = moonFinishPosition;
                _moonImageView.frame = moonFrame;
        }];

        [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.6f animations:^{
            _flameImageView.transform = CGAffineTransformMakeScale(1.f, 1.f);
        }];
    } completion:nil];

问题是当我第一次加载ViewController时,动画会播放,但是当完成时它会快速回到起始位置。但是,如果我离开ViewController并再次返回,则播放动画并且更改仍然存在。

我的问题

我的子视图是否播放了两次?

我什么时候可以确定我的ViewController已经完成加载?

1 个答案:

答案 0 :(得分:0)

假设not-autolayout,在完成块中,将视图属性设置为最终的所需状态......

// taking the rocket frame as an example:
CGRect rocketFrame = _rocketImageView.frame;  // do this with the other frames, too

[UIView animateKeyframesWithDuration:2.5f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:1.0f animations:^{

            rocketFrame.origin.y = rocketFinishPosition;
            _rocketImageView.frame = rocketFrame;

            CGRect flameFrame = _flameImageView.frame;
            flameFrame.origin.y = rocketFinishPosition + rocketFrame.size.height - 3;
            _flameImageView.frame = flameFrame;
            _flameImageView.hidden = false;

            CGRect earthFrame = _earthImageView.frame;
            earthFrame.origin.y = earthFinishPosition;
            _earthImageView.frame = earthFrame;

                CGRect moonFrame = _moonImageView.frame;
                moonFrame.origin.y = moonFinishPosition;
                _moonImageView.frame = moonFrame;
        }];

        [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.6f animations:^{
            _flameImageView.transform = CGAffineTransformMakeScale(1.f, 1.f);
        }];
    } completion:^(BOOL finished) {
         // do this with the other frames, too
        _rocketImageView.frame = rocketFrame;
    }];