ios - 动画 UICollectionViewCell 时 bounds.size 和 frame.size 之间的相关性是什么

标签 ios objective-c animation uicollectionviewcell bounds

我读过几篇文章(如 https://stackoverflow.com/a/5340339/1084822https://stackoverflow.com/a/15582466/1084822),要使用动画调整 UIView 的大小,我们不能为框架设置动画,我们必须混合使用 bounds.size 和 bounds。起源和位置。

但是,我找不到这些属性与框架的属性之间的相关性。

我有一个带有自定义布局的 UICollectionView。当一个单元格被删除时,布局被刷新并且每个单元格通过 prepareLayoutlayoutAttributesForElementsInRect: 方法获得它的新框架。然后,调用 finalizeCollectionViewUpdates 方法(我们可以在其中编辑动画),最后单元格移动并调整大小。

我已经通过单元格动画的反复试验进行了大量探索,以下是我的发现:

  • 我的可见单元格默认设置了 3 个动画,它们是 position、bounds.origin 和 bounds.size
  • 当我更改这些动画的 from/to 值时,只有单元格的 POSITION 会受到影响,而不会影响其 SIZE

详细结果

默认值

当我坚持使用动画的默认值并使用这些日志检查它们的值时,

- (void)finalizeCollectionViewUpdates {
    for(UICollectionViewCell* cell in [self.collectionView visibleCells]) {
        NSLog(@"cell: %@", cell);
        for(NSString* key in cell.layer.animationKeys) {
            CABasicAnimation* animation = (CABasicAnimation*)[cell.layer animationForKey:key];
            NSLog(@"animation %@: from %@ to %@", key, animation.fromValue, animation.toValue);
        }
    }
}

我得到的是收缩单元格(它的框架代表它的最终状态):

cell: <PlayQueueSongCell: 0x7fbf1bc8ebe0; baseClass = UICollectionViewCell; frame = (53.3333 80; 480 480); opaque = NO; animations = { position=<CABasicAnimation: 0x7fbf1bf54e20>; bounds.origin=<CABasicAnimation: 0x7fbf1bf556b0>; bounds.size=<CABasicAnimation: 0x7fbf1bf557a0>; }; layer = <CALayer: 0x7fbf1bc8e9f0>>
animation position: from NSPoint: {666.66666666666674, 0} to NSPoint: {0, 0}
animation bounds.origin: from NSPoint: {0, 0} to NSPoint: {0, 0}
animation bounds.size: from NSSize: {160, 160} to NSSize: {0, 0}

这是扩展单元格(它的框架代表它的最终状态):

cell: <PlayQueueSongCell: 0x7fbf1bd6cd00; baseClass = UICollectionViewCell; frame = (640 0; 640 640); opaque = NO; animations = { position=<CABasicAnimation: 0x7fbf1bf55b70>; bounds.origin=<CABasicAnimation: 0x7fbf1bf55e20>; bounds.size=<CABasicAnimation: 0x7fbf1bf55f10>; }; layer = <CALayer: 0x7fbf1bd73200>>
animation position: from NSPoint: {666.66666666666652, 0} to NSPoint: {0, 0}
animation bounds.origin: from NSPoint: {0, 0} to NSPoint: {0, 0}
animation bounds.size: from NSSize: {-160, -160} to NSSize: {0, 0}

以慢动作查看结果: enter image description here

没有 bounds.size 动画

当我用这段代码删除 bounds.size 动画时,

- (void)finalizeCollectionViewUpdates {
    for(UICollectionViewCell* cell in [self.collectionView visibleCells]) {
        for(NSString* key in cell.layer.animationKeys) {
            [cell.layer removeAnimationForKey:@"bounds.size"];
        }
    }
}

我可以看到只有单元格的位置受到影响... enter image description here

bounds.size动画从(0,0)到(1000,1000)

当我使用这段代码将 bounds.size 的值更改为 (0,0) -> (1000,1000) 时:

- (void)finalizeCollectionViewUpdates {
    for(UICollectionViewCell* cell in [self.collectionView visibleCells]) {
        for(NSString* key in cell.layer.animationKeys) {
            [cell.layer removeAnimationForKey:@"bounds.size"];
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
            [animation setFromValue:[NSValue valueWithCGSize:CGSizeZero]];
            [animation setToValue:[NSValue valueWithCGSize:CGSizeMake(1000, 1000)]];
            [animation setDuration:0.3];
            [cell.layer addAnimation:animation forKey:@"bounds.size"];
        }
    }
}

我仍然可以看到只有单元格的位置受到影响... enter image description here

问题

由于单元格在动画开始之前获得其新帧并且 bounds.size 不影响单元格的大小,所以我找不到一种方法来平滑地改变它的大小。

大家都在谈论的 bounds.size 和 frame.size 之间的关系是什么,如何动画化单元格的大小?

最佳答案

我找到的解决方案是首先删除 bounds.size 动画,因为它以不需要的方式移动单元格。然后,我创建了一个 transform.scale 动画,它以一个模拟其先前大小的值开始,以 1 的值结束。

if([cell.layer animationForKey:@"bounds.size"]) {
    [cell.layer removeAnimationForKey:@"bounds.size"];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.3;
    if(CGSizeEqualToSize(cell.frame.size, self.itemSize)) { //shrinking cell
        animation.fromValue = @1.333;
        animation.toValue = @1;
    } else {                                                //expending cell
        animation.fromValue = @0.75;
        animation.toValue = @1;
    }
    [cell.layer addAnimation:animation forKey:@"transform.scale"];
}

关于ios - 动画 UICollectionViewCell 时 bounds.size 和 frame.size 之间的相关性是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35140258/

相关文章:

ios - DynamoDB 获取项目计数

java - 如何在android中将src设置为relativeLayout?

ios - 在 Swift 中为 DebugPrintable 实现 debugDescription

javascript - ios/android webview 中的 Localstorage 持久化

objective-c - 查找适当的 key 以用于验证与 Objective-C 的 SSH 连接

ios - 如何使用 UIScrollView 实现 UIPageViewController?

ios - 从中间动画UIImageView翻转

java swing ios 喜欢滑动菜单

ios - POST请求ios的安全性

ios - 从 iOS8 上的 Bundle ID 获取应用程序的名称