1

我正在使用这种布局的自动布局:

在此处输入图像描述

带框架的self.view (0,80, 320, 488)

带框架的 collectionView (0,0,320,220)

underView 带框架 (0,220,320,268)

并尝试实现一个动画,该动画应该调整 underView 的大小以扩大其高度,以便 underView 部分覆盖 collectionView 自下而上的移动。

Autolayout 为这两个视图强制设置高度约束,因此我为 underView 高度约束创建了一个 IBOutlet,我在动画中对其进行了调整:

NSLog(@"underView height %.2f", self.underView.frame.size.height);

NSLog(@"offset (%.2f, %.2f)", offset.x, offset.y);

heightConstraint.constant += offset.y;

NSLog(@"heightConstraint %.2f", heightConstraint.constant);

[self.underView setNeedsUpdateConstraints];

[UIView animateWithDuration:ANIMATION_DURATION
                      delay:ANIMATION_DELAY
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{

                     [self.underView layoutIfNeeded];

                     NSLog(@"underView height %.2f", heightConstraint.constant);

                 } completion:nil];

在控制台中我可以看到:

2013-07-30 09:09:37.268 Cal[49611:a0b] underView height 268.00
2013-07-30 09:09:37.268 Cal[49611:a0b] offset (320.00, 144.00)
2013-07-30 09:09:37.269 Cal[49611:a0b] heightConstraint 412.00
2013-07-30 09:09:37.270 Cal[49611:a0b] underView height 412.00

但是调整大小根本不会发生。

我能想到的这种行为的唯一原因是上面 collectionView 的高度限制,但由于我不希望集合重新加载,所以我无法对其进行操作。

我想知道如何实现让underView放大超过collectionView?我尝试使用约束优先级,但没有奏效......

4

2 回答 2

2

根据this answer尝试以下操作。确保调用layoutIfNeededsuperview:

[UIView animateWithDuration:ANIMATION_DURATION
                      delay:ANIMATION_DELAY
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{

                     heightConstraint.constant += offset.y;
                     [self.view layoutIfNeeded];

                     } 
                 completion:nil];
于 2013-07-30T08:56:05.490 回答
0

所以,我找到了一个可能的解决方案来解决我的问题,但不确定它是否是最好的解决方案。我缺少对 self.collectionView 的垂直空间约束的引用,所以最终代码是:

verticalSpace.constant -= shift;
heightConstraint.constant += shift;

[self.collectionView setNeedsUpdateConstraints];
[self.underView setNeedsUpdateConstraints];

[UIView animateWithDuration:ANIMATION_DURATION
                      delay:ANIMATION_DELAY
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [self.view layoutIfNeeded];
                 } completion:nil
 ];

我将等待其他更好的解决方案一段时间,这样我就不会将自己的答案标记为已接受的答案。

于 2013-07-30T13:03:23.947 回答