1
votes

Fixed

It turns out the animation was being called twice. I've now fixed it so the animation is only called once and it works perfectly.


I suspect it might be something related to my use of child view controllers, but I'm noticing some strange behaviour when I try to remove the child view controller's view with an animation.

I'm adding the child view controller to the parent, along with the child view controller's view, and animating that view from the bottom of the screen. This works perfectly — my issue is when I try to remove the child view/view controller. I'm animating the child's view to the bottom of the screen, and then calling [self.view removeFromSuperview] in the completion block, but the view is removed immediately (so no animation takes place). If I delete the [self.view removeFromSuperview] line, the animation works correctly, but then the view isn't removed from the parent view controller's view.


Adding the child view controller

(this works as intended)

ChildViewController *myChildViewController = [[ChildViewController alloc] init];
myChildViewController.view.frame = CGRectMake(0.f, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 64.f);

[self addChildViewController:myChildViewController];
[self.view addSubview:myChildViewController.view];
[myChildViewController didMoveToParentViewController:self];

[UIView animateWithDuration:0.75f delay:0.f usingSpringWithDamping:0.6f initialSpringVelocity:0.75f options:0 animations:^{
    myChildViewController.view.frame = CGRectMake(0.f, 64.f, self.view.frame.size.width, self.view.frame.size.height - 64.f);
} completion:nil];


Removing the child view controller

(this doesn't work — the view is removed immediately, instead of after completion)

[UIView animateWithDuration:0.75f delay:0.f usingSpringWithDamping:0.6f initialSpringVelocity:0.75f options:0 animations:^{
    self.view.frame = CGRectMake(0.f, self.view.superview.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
    if (finished)
    {
        [self willMoveToParentViewController:self.parentViewController];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }
}];



I've even tried removing [self willMoveToParentViewController:self.parentViewController] and [self removeFromParentViewController] just to see if that changed anything, but as long as [self.view removeFromSuperview] is there, the view disappears immediately.

1
Are you calling this method only once? I once had the same problem, turns out that the second animation finished immediately because the other one was still going on, and removed the view.Scott Berrevoets
Ah! You're right! I just checked and the completion block runs twice, immediately. Time to figure out which other part of my code is calling it twice. Thanks!dvyio
Sounds like it's time to add a breakpoint or two :)Eric Goldberg
Try giving the delay under UIView animateWithDuration:0.75f delay:4.0f usingSpringWithDamping:0.6f initialSpringVelocity:0.75f options:0 animations : to some number like 4.0f. And checkChetan

1 Answers

0
votes

Add the in the Parent class

- (void) moveToAddJobVC:(NSString*)storyboardId {

    ChildViewController *destVC = [self.storyboard instantiateViewControllerWithIdentifier:storyboardId];
    destVC.completionHandler = ^(NSString* string) {
        NSLog(@"Returned strng.....%@",string);
        //[self viewWillAppear:YES];
    };
    [destVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self addChildViewController:destVC];
    [self.view addSubview:destVC.view];
    [destVC didMoveToParentViewController:self];

    [UIView animateWithDuration:0.3 animations:^{
        [destVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];

}

And in child VC write

@property (nonatomic,copy) void (^completionHandler)(NSString *string);

- (IBAction)dismissBtnAction:(id)sender{

    if (self.completionHandler)
    {
        self.completionHandler(@"Return data");
    }
    [self removeFromParentViewController];
    [self.view removeFromSuperview];
}