jacob
jacob

Reputation: 541

Masonry animationWithDuration doesn't animate

I am trying to animate a UIWebView in from the bottom on top of my viewController, and I am using masonry (https://github.com/Masonry/Masonry).

I initially create my webview with the size of 0 - (x,y,height and width), and I then try to animate it so that the webview animates "on top" of the viewcontroller. The webview is shown, but it doesn't animate in place - it just appears immediately. Can anyone who has experience guide me in the right direction?

This is my button action

-(void)didPressBtn{
    self.infoView = [UIWebView new];
    self.infoView.scalesPageToFit = YES;
    self.infoView.backgroundColor = [UIColor whiteColor];
    self.infoView.delegate = self;
    [self.scrollView addSubview:self.infoView];
    [self.infoView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];


    [self.scrollView layoutIfNeeded];
    //FIXME: Hmmm it doesn't really animate!?
    [UIView animateWithDuration:1 animations:^{
        [self.scrollView setContentOffset:CGPointMake(0, 0)];
        [self.infoView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.scrollView);
        }];

        [self.scrollView layoutIfNeeded];

    } completion:^(BOOL finished) {
                [self.infoView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:NSLocalizedString(@"INFORMATION_URL", )] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15]];
    }];
}

My viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView = [UIScrollView new];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    UIView *contentView = [UIView new];
    [self.scrollView addSubview:contentView];

    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];

//Then adding buttons and such...
}

Upvotes: 3

Views: 5703

Answers (1)

Ben Packard
Ben Packard

Reputation: 26506

One quick observation regarding didPressButton. You are using mas_makeConstraints to set the edges equal to @0 and them immediately afterwards using mas_makeConstraints again to change them so that the scrollview is filled. This adds contradictory constraints on top of the first set.

Instead, you can use mas_remakeConstraints to replace the existing constraints on that view.

As for the animation, the pattern I use is to first update the constraints (not inside an animation block) and then animate a layout:

[UIView animateWithDuration:1
        animations:^{
            [theParentView layoutIfNeeded];
        }];

See also How do I animate constraint changes?

Upvotes: 17

Related Questions