I have a simple animation that moves a slide up and down repeatedly. Here is the code:

    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];

This is pretty straight forward and works great; however, when I am presenting the view (self presentViewController:animated:) the animation does not happen.

In fact, the slider is INSTANTLY moved to the lower position (as if it called the animation only once and moved it back down, but not back up).

I have also tried this:

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

This will keep the slider in its original position instead of moving down once, and the animation still does not happen.

Any idea what I am missing? Thanks!

有帮助吗?

解决方案

Your animation works. I tested it.

What you need is:

Instead of using:

self presentViewController:animated:

Use this:

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

Addendum:

In your view controller's viewDidAppear, add the following code:

- (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];

}

It will animate when it comes into view.

I ran it as a test a priori. Hope it helps.

其他提示

On 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)
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top