1

I have a UIView that flies from the top when I open that view controller. I've set the Y Constraint of the UIView to -200 and when the view loads, the below is called and everything works fine:

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    self.popUpViewYConstraint.constant = 37.0f;
    self.closeButtonYConstraint.constant = 28.0f;
    [self.popUpBaseView setNeedsUpdateConstraints];
    [self.closeButton setNeedsUpdateConstraints];

    [UIView animateWithDuration:0.25f animations:^{
        [self.popUpBaseView layoutIfNeeded];
        [self.closeButton layoutIfNeeded];
        [self.view layoutIfNeeded];
    }];
}

But now I have a close button that should animate the UIView back to the -200 position and then remove the view controller from the screen. But this animation is not taking place. The view controller is getting removed directly. Here's what I'm doing:

- (IBAction)closePressed:(id)sender
{
    NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
    self.navigationController.viewControllers = navigationArray;

    [UIView animateWithDuration:2.0f animations:^{
        self.popUpViewYConstraint.constant = -200.0f;
        [self.popUpBaseView layoutIfNeeded];
    } completion:^(BOOL finished){
        [navigationArray removeObjectAtIndex: 1];
        [self.baseView removeFromSuperview];
        [self.view removeFromSuperview];
    }];
}

I referred to this link. It seems to be working for them but doesn't work for me. Please help.

Community
  • 1
  • 1
Anil
  • 2,430
  • 3
  • 37
  • 55
  • What happens if you remove the code that removes the views? (i.e. just make the completion block blank). Also, try this... `animateWithDuration:delay:options:animations:completion` instead of the one you're using with options... UIViewAnimationOptionBeginFromCurrentState – Fogmeister Jan 16 '14 at 17:50
  • Nothing happens. View Controller is just removed without animation. – Anil Jan 16 '14 at 17:52
  • Can you put a screenshot of before and after the animation that makes the view appear. – Fogmeister Jan 16 '14 at 17:56
  • @anil, did you found the answer? Seems I have the same problem, but instead of a view controller I am animating a UIView. And the strange thing is, when the UIView has no subviews it animates and when I add subview/s(a UILabel) the animation just stops working. – BangOperator Apr 13 '14 at 17:20
  • @somexyz I finally did the animation based on the frame of the UIView. that solved the issue for me. – Anil Apr 14 '14 at 05:56
  • @Anil, I found the answer to my question :) by animating the constraint change itself. – BangOperator Apr 16 '14 at 07:37

2 Answers2

0

This line:

self.popUpViewYConstraint.constant = -200.0f;

Should be called before the animation block. Also i'm not sure about hierarchy of your views, but make sure you are calling the correct view with layoutIfNeeded call.

tadasz
  • 4,366
  • 5
  • 26
  • 33
  • Tried doing that. No difference. Still not working. And as far as the view hierarchy goes. I have the self.view then a self.baseView and then self.popUpBaseView. – Anil Jan 16 '14 at 17:47
  • Yeah, you can call this either inside or before the animation block. It won't make a difference. – Fogmeister Jan 16 '14 at 17:48
  • @Fogmeister is there anything else I can do to create this animation? – Anil Jan 16 '14 at 17:50
0

How about this

- (IBAction)closePressed:(id)sender
{
    NSMutableArray *navigationArray = [[NSMutableArray alloc]:initWithArray:self.navigationController.viewControllers];
    self.navigationController.viewControllers = navigationArray;

    // I am sure the constant should be set outside the animation block. It won't happen until next run loop, which will be inside the block. 
    self.popUpViewYConstraint.constant = -200.0f;
    // setNeedsUpdateConstraints should be called on the view to which the constraint is added, not the view affected by the constraint.
    [self.baseView setNeedsUpdateConstraints];

    [UIView animateWithDuration:2.0f animations:^{
         // I think this should be topmost view
         [self.view layoutIfNeeded];
    } completion:^(BOOL finished){
        [navigationArray removeObjectAtIndex: 1];
        [self.baseView removeFromSuperview];
        [self.view removeFromSuperview];
    }];
 }
Dean Davids
  • 4,174
  • 2
  • 30
  • 44
  • I think I have a constraint that's not allowing this animation from happening. Think that's what's stopping this from happening. – Anil Jan 16 '14 at 18:51