【问题标题】:Custom UICollectionViewCell contentView animation自定义 UICollectionViewCell contentView 动画
【发布时间】:2023-03-16 16:51:01
【问题描述】:

如果用户触摸它,我会更改 UICollectionViewCell 的高度。为此,我使用performBatchUpdates: 更改基础数据并使用标准单元格动画。这完美地工作,并且使用标准的增长和收缩动画来动画变化。

此外,我使用reloadSection 更新该部分中单元格的所有子视图。这是我的代码:

[collectionView performBatchUpdates:^{
    dataHelper.isCellExpanded = !dataHelper.isCellExpanded;
 } completion:^(BOOL finished) {
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}];

我需要为展开的单元格状态实现子视图的灵活重新排序。所以我不使用自动布局。我只更改子视图框架,并希望将框架更改与UICollectionViewCell 动画一起制作动画。

问题是单元格的contentView 内的子视图在没有动画的情况下更新,并且单元格的高度变化动画完成后。但我想动画单元格子视图以及高度变化的动画。如何实现?

更新

我的UICollectionViewCell 中的自定义动画方法如下所示:

- (void)animate {
 [UIView animateWithDuration:1.0
                       delay:0.0
                     options:UIViewAnimationOptionCurveLinear
                  animations:^
 {
     CGRect labelFrame = self.testLabel.frame;
     labelFrame.origin.y += 70;
     self.testLabel.frame = labelFrame;
 }
 completion:nil];
}

我在我的UICollectionViewCell 子类的initWithFrame: 中创建testLabel,如下所示:

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
      CGRect cellFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);

      // content
      _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 20, frame.size.width - 20, 20)];
      _testLabel.backgroundColor = [UIColor redColor];
      _testLabel.textColor = [UIColor blackColor];
      _testLabel.text = @"test";
      [self.contentView addSubview:_testLabel];
      ....

【问题讨论】:

    标签: ios objective-c animation uicollectionview uicollectionviewcell


    【解决方案1】:

    您必须在集合视图单元格中实现一个动画方法,并且您需要从 performBatchUpdates 方法中调用该方法。

    [collectionView performBatchUpdates:^{
        dataHelper.isCellExpanded = !dataHelper.isCellExpanded;
        // Access the cells you want to animate
        yourCustomCell *cell = (yourCustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [cell animate];
    } completion:^(BOOL finished) {
        [collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
    }];
    

    在单元格动画方法中,您可以实现您可能想要的所有动画。

    【讨论】:

    • 这没有任何作用。我在我的单元格中添加了一个animate 方法。在这个方法中,我称之为标准animateWithDuration。代码被调用,但没有动画。
    • 你的动画实现是什么样的?
    【解决方案2】:

    我可以通过在UICollectionViewCell 子类中的contentViewtestLabel 中以autoresizingMask 的形式添加弹簧和支柱来解决这个问题

    - (id)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
      if (self) {
          CGRect cellFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    
          self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
          // content
          _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 20, frame.size.width - 20, 20)];
          _testLabel.backgroundColor = [UIColor redColor];
          _testLabel.textColor = [UIColor blackColor];
          _testLabel.text = @"You rock";
          _testLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
          [self.contentView addSubview:_testLabel];
    

    旁注: 我一直在使用 AutoLayout 来解决这个问题,但一直无法让它工作。

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 2018-09-02
      • 1970-01-01
      • 2017-04-02
      • 2018-06-03
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多