【问题标题】:Animating the height change of a UICollectionView动画 UICollectionView 的高度变化
【发布时间】:2015-08-13 14:41:49
【问题描述】:

我有一个UICollectionView,它是UIScrollViewsubview(这是必需的,因为集合视图上方和下方的内容。

无论如何...

集合视图每行显示三个项目,开始时只有两行。

前五个项目是实际项目,最后一个项目是“查看更多”项目。

当最后一个项目被按下时,我会更改显示的项目数并更新集合视图...

NSMutableArray *indexPaths = [NSMutableArray array];

for (int i = 5 ; i<self.facilities.count ; i++) {
    [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
}

// changing the tableCollapsed bool updates the number of items
if (self.isTableCollapsed) {
    self.tableCollapsed = NO;
    [self.collectionView insertItemsAtIndexPaths:indexPaths];
} else {
    self.tableCollapsed = YES;
    [self.collectionView deleteItemsAtIndexPaths:indexPaths];
}

然后我跑……

[self setHeightAnimated:YES];

这是干什么的……

- (void)setHeightAnimated:(BOOL)animated
{
    NSInteger numberOfRows = (NSInteger) ceilf((CGFloat)[self getNumberOfItems] / self.numberOfItemsPerRow);

    self.heightConstraint.constant = numberOfRows * self.rowHeight;

    if (animated) {
        [UIView animateWithDuration:0.2
                         animations:^{
                             [self layoutIfNeeded];
                         }];
    } else {
        [self layoutIfNeeded];
    }
}

这总是有效并设置正确的高度,当它应该是YESNO 时,它会得到正确的animated 值。但是,它从不动画。它只是立即切换到新的高度。

我猜这是因为运行了一些布局代码或其他原因,但我想知道是否有人知道如何最好地做到这一点,以便高度随着我指定的动画正确变化。

谢谢

【问题讨论】:

    标签: ios objective-c autolayout uicollectionview


    【解决方案1】:

    好的!几个小时后,我找到了解决方案。

    我已将插入块移到这样的方法中...

    - (void)addItemsAtIndexPaths:(NSArray *)indexPaths {
        [UIView performWithoutAnimation:^{
            [self.collectionView insertItemsAtIndexPaths:indexPaths];
        }];
        [self setHeightAnimated:YES completion:nil];
    }
    

    这使用了一种我不知道performWithoutAnimation 的方法将项目添加到集合视图中。然后我独立地为高度设置动画。

    像魅力一样工作。

    反过来也可以……

    - (void)removeItemsAtIndexPaths:(NSArray *)indexPaths {
        [self setHeightAnimated:YES completion:^(BOOL finished) {
            [UIView performWithoutAnimation:^{
                [self.collectionView deleteItemsAtIndexPaths:indexPaths];
            }];
        }];
    }
    

    这首先对高度进行动画处理,然后删除没有动画的项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2015-02-27
      • 2018-09-13
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      相关资源
      最近更新 更多