呈现的ViewController忽略了UIView动画

时间:2014-01-30 23:28:37

标签: ios7 uiviewanimation presentviewcontroller

我有一个简单的动画,可以反复上下移动幻灯片。这是代码:

    CGRect frm_down = slider.frame;
    frm_down.origin.y += 50;

    [UIView animateWithDuration:0.7
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                     animations:^{
                         slider.frame = frm_down;
                     }
                     completion:NULL];

这很直接,效果很好;但是,当我呈现视图(self presentViewController:animated :)时,动画不会发生。

实际上,滑块立即移动到较低位置(就好像它只调用一次动画并将其向下移动,但不会向后移动)。

我也试过这个:

CGRect frm_down = [[[slider layer] presentationLayer] frame]

这会使滑块保持原位而不是向下移动一次,动画仍然不会发生。

知道我错过了什么吗?谢谢!

2 个答案:

答案 0 :(得分:0)

你的动画有效。我测试了它。

您需要的是:

而不是使用:

self presentViewController:animated:

使用此:

[self addChildViewController:<YOUR_VIEWCONTROLLER>];
[self.view addSubview:<YOUR_VIEWCONTROLER>.view];
[<YOUR_VIEWCONTROLER> didMoveToParentViewController:self];

附录:

在视图控制器的viewDidAppear中,添加以下代码:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGRect frm_down = self.view.frame;
    frm_down.origin.y += 50;

    [UIView animateWithDuration:0.7
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                     animations:^{
                         self.view.frame = frm_down;
                     }
                     completion:NULL];

}

当它进入视野时会有动画。

我把它作为先验测试。希望它有所帮助。

答案 1 :(得分:0)

在Swift上:

override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(true)

    var from_down =  self.view.frame
    from_down.origin.y += 50

    UIView.animate(withDuration: 0.2, delay: 0.0, options: .autoreverse, animations: {
        self.view.frame = from_down
        }, completion: nil)
}
相关问题