iOS自动布局动画在iOS 7上不起作用,但适用于iOS 8

时间:2014-11-14 09:45:49

标签: ios ios7 ios8 autolayout

我正在尝试动画从左侧滑动并使用整个空间的全屏视图。

我有以下代码可以在iOS 8上完成预期的工作,但在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];

}];

当在iOS 7上执行此操作时,destination.view会立即添加到屏幕上,而不会显示任何动画。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

您的动画块只应包含[source.view layoutIfNeeded];(请参阅How do I animate constraint changes?)。

您应该在块之前更改约束,如下所示:

[source.view layoutIfNeeded];

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

[UIView animateWithDuration:.35f animations:^{

    [source.view layoutIfNeeded];

}];