自动布局和嵌套动画

时间:2013-04-11 06:46:16

标签: iphone ios uiviewanimation autolayout

我有以下简单的嵌套动画代码,用于转换,如附图所示。

[UIView animateWithDuration:2.0f animations:^{
    CGRect frame = self.label.frame;
    frame.origin.y += 100;
    self.label.frame = frame;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:2.0f animations:^{
        self.button.transform = CGAffineTransformMakeRotation(M_PI);
    }completion:^(BOOL finished) {
        //
    }];
}];

intended animation transition

视图布局在具有自动布局的XIB文件中定义。当IB自动放置它时,它对按钮和标签都有垂直约束。

在这种情况下,转换不会如附图所示。它从State1到State2,但是简单地返回到State1 ,然后转到State3。

我认为这是由标签的垂直约束引起的。当我在动画之前以编程方式删除约束时,动画开始按预期工作。

[self.view removeConstraint:self.labelConstraint];

我想知道的是,这是正确方式。一些post表明,由于约束不足以确定布局,删除约束有时会导致不可预测的行为。

1 个答案:

答案 0 :(得分:0)

好的,我的朋友回答了我的问题。

[UIView animateWithDuration:2.0f animations:^{
    CGRect frame = self.label.frame;
    frame.origin.y += 100;
    self.label.frame = frame;
} completion:^(BOOL finished) {

    ////////////////////////////////////
    self.labelConstraint.constant += 100;
    ////////////////////////////////////

    [UIView animateWithDuration:2.0f animations:^{
        self.button.transform = CGAffineTransformMakeRotation(M_PI);
    }completion:^(BOOL finished) {
        //
    }];
}];

请注意,在第二个动画开始之前,我正在更改约束值以适应新帧。这样就会有足够的自动布局。

如果有人拥有它们,还在等待其他方式!