简体   繁体   中英

iOS autolayout animation doesn't work on iOS 7 but works on iOS 8

I am trying to animate a full-screen view sliding in from the left and using the whole space.

I have the following code which does the expected job perfectly fine on iOS 8, but fails to do so on iOS 7 :

[source.view addSubview:destination.view];

NSDictionary *viewsDict = @{@"tableView":destination.tableView,
                            @"tapView": destination.tapView,
                            @"destinationView": destination.view};

[source.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[destinationView]|" options:0 metrics:nil views:viewsDict]];

source.leftConstraint = [NSLayoutConstraint constraintWithItem:destination.tableView
                                         attribute:NSLayoutAttributeLeft
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:source.view
                                         attribute:NSLayoutAttributeLeft
                                        multiplier:1.0
                                          constant:-source.view.bounds.size.width];

[source.view addConstraint:source.leftConstraint];

source.rightConstraint = [NSLayoutConstraint constraintWithItem:source.view
                                         attribute:NSLayoutAttributeTrailing
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:destination.tapView
                                         attribute:NSLayoutAttributeTrailing
                                        multiplier:1.0
                                          constant:source.view.bounds.size.width];

[source.view addConstraint:source.rightConstraint];

[source.view layoutIfNeeded];

[UIView animateWithDuration:.35f animations:^{

    source.rightConstraint.constant = 0;
    source.leftConstraint.constant = 0;

    [source.view layoutIfNeeded];

}];

When this gets executed on iOS 7 the destination.view gets added to the screen straight away without any animation. Any idea why that might be the case ?

Your animation block should only contain [source.view layoutIfNeeded]; (see How do I animate constraint changes? ).

You should change the constraints before the block, like this :

[source.view layoutIfNeeded];

source.rightConstraint.constant = 0;
source.leftConstraint.constant = 0;

[UIView animateWithDuration:.35f animations:^{

    [source.view layoutIfNeeded];

}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM