什么是动画UICollectionViewCell时bounds.size和frame.size之间的相关性 [英] What is the correlation between bounds.size and frame.size when animating UICollectionViewCell
问题描述
我读过的几个职位(如 http://stackoverflow.com/a/5340339/1084822 并 http://stackoverflow.com/a/15582466/1084822 ),要调整动画一个UIView,我们不能动画框架,我们必须bounds.size,bounds.origin和位置的混合做。
I've read on several posts (like http://stackoverflow.com/a/5340339/1084822 and http://stackoverflow.com/a/15582466/1084822) that to resize a UIView with animation, we can't animate the frame and we must do it with a mix of bounds.size, bounds.origin and position.
不过,我找不到什么这些属性和那些帧之间的相关性。
However, I can't find what is the correlation between those properties and those of the frame.
我有一个自定义布局的UICollectionView。当细胞被删除,布局被刷新,每一个细胞通过 prepareLayout
和 layoutAttributesForElementsInRect获得其新的框架:
方法。然后, finalizeCollectionViewUpdates
方法(在这里我们可以编辑动画)被调用,最后细胞,并开始调整。
I have a UICollectionView with a custom layout. When a cell is deleted, the layout is refreshed and every cell gets its new frame through the prepareLayout
and layoutAttributesForElementsInRect:
methods. Then, the finalizeCollectionViewUpdates
method (where we can edit animations) gets called and finally the cells move and get resized.
我已经通过试错与细胞动画做了很多的探索,这里是我发现了什么:
I've done a lot of exploration by trial and error with the cells animation and here is what I found :
- 3动画默认设置为我这是位置,bounds.origin和bounds.size 可见单元格
- 当我改变了从/到这些动画的值,只有单元格的位置受到影响,从来没有它的大小
当我坚持到动画的默认值,并检查它们的值与这些日志,
When I stick to the default values of the animations and check their values with these logs,
- (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);
}
}
}
我得到这个为在缩小细胞(其框架重新present最终状态):
I get this for the shrinking cell (its frame represent its final state):
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}
和本作的花费细胞(其框架重新present最终状态):
and this for the expending cell (its frame represent its final state):
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}
当我删除bounds.size动画与code的此块,
When I remove the bounds.size animation with this block of code,
- (void)finalizeCollectionViewUpdates {
for(UICollectionViewCell* cell in [self.collectionView visibleCells]) {
for(NSString* key in cell.layer.animationKeys) {
[cell.layer removeAnimationForKey:@"bounds.size"];
}
}
}
而当我改变bounds.size的值(0,0) - 与code此块>(1000,1000):
And when I change the values of bounds.size to (0,0) -> (1000,1000) with this block of code:
- (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"];
}
}
}
由于细胞中获取新的帧就在动画开始前和bounds.size不影响细胞的大小,我无法找到一个方法来平滑地改变它的大小。
Since the cell gets its new frame just before the animation starts and the bounds.size does not affect the cell's size, I can't find a way to change its size smoothly.
什么是bounds.size和frame.size之间的关系,每个人都在谈论和一个如何动画单元格的大小?
What is the correlation between the bounds.size and the frame.size that everyone is talking about and how can one animate the size of the cell?
推荐答案
我发现是因为它把细胞以意外方式先删除bounds.size动画解决方案。然后,我创建与将要模拟其previous大小和值1停止值启动transform.scale动画。
The solution I found is to first remove the bounds.size animation since it moves the cell in an unwanted way. Then, I create a transform.scale animation that starts with a value that will simulate its previous size and stops with a value of 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"];
}
这篇关于什么是动画UICollectionViewCell时bounds.size和frame.size之间的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!