Ios 为UICollectionView设置新的collectionViewLayout提供空白屏幕

Ios 为UICollectionView设置新的collectionViewLayout提供空白屏幕,ios,uicollectionview,Ios,Uicollectionview,我开始尝试自定义UICollectionView布局。我的UIViewController中有一个UICollectionView,它符合UICollectionViewDataSource和UICollectionViewFlowLayout协议。每个单元格只是一个图像和一个标签。我有一个按钮可以这样做: - (IBAction)switchLayout:(id)sender { PinchLayout *pinchLayout = [[PinchLayout alloc] init]

我开始尝试自定义UICollectionView布局。我的UIViewController中有一个UICollectionView,它符合UICollectionViewDataSource和UICollectionViewFlowLayout协议。每个单元格只是一个图像和一个标签。我有一个按钮可以这样做:

- (IBAction)switchLayout:(id)sender {
    PinchLayout *pinchLayout = [[PinchLayout alloc] init];
    [self.collectionView setCollectionViewLayout:pinchLayout];
}
PinchLayout直接从苹果的示例代码中复制而来。调用switchLayout:方法时,我的collectionView将消失。我不明白为什么。我以为它会活跃到新的位置。或者在这种情况下,除了为新布局添加手势支持之外,不要做任何事情。然后,当我再次按下按钮时,我看到一个新的布局,其中我的所有集合视图单元格都缩小为其原始大小。这是平铺

-(void)applyPinchToLayoutAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes
{
    if ([layoutAttributes.indexPath isEqual:self.pinchedCellPath])
    {
        layoutAttributes.transform3D = CATransform3DMakeScale(self.pinchedCellScale, self.pinchedCellScale, 1.0);
        layoutAttributes.center = self.pinchedCellCenter;
        layoutAttributes.zIndex = 1;
    }
}

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect];

    for (UICollectionViewLayoutAttributes* cellAttributes in allAttributesInRect)
    {
        [self applyPinchToLayoutAttributes:cellAttributes];
    }

    return allAttributesInRect;
}

-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes* attributes = [super layoutAttributesForItemAtIndexPath:indexPath];

    [self applyPinchToLayoutAttributes:attributes];

    return attributes;
}

-(void)setPinchedCellScale:(CGFloat)scale
{
    _pinchedCellScale = scale;
    [self invalidateLayout];
}

- (void)setPinchedCellCenter:(CGPoint)origin {
    _pinchedCellCenter = origin;
    [self invalidateLayout];
}
有什么想法吗?谢谢

编辑:如果我将操作方法更改为

- (IBAction)switchLayout:(id)sender {
    PinchLayout *pinchLayout = [[PinchLayout alloc] init];
    [self.collectionView setCollectionViewLayout:pinchLayout animated:YES];
}
我看着我所有的收藏视图单元格从屏幕转到顶部。不知道他们为什么这么做

编辑2:我记录了属性和单元格属性,看起来帧总是在屏幕的边界内,所以我仍然不确定为什么单元格会从屏幕转到顶部。这是日志:

2012-12-04 21:54:40.808 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7516450> index path: (<NSIndexPath 0xe111180> 2 indexes [0, 4]); frame = (297.2 100; 50 50); 
2012-12-04 21:54:40.809 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7611df0> index path: (<NSIndexPath 0x76236e0> 2 indexes [0, 0]); frame = (50 100; 50 50); 
2012-12-04 21:54:40.812 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76235e0> index path: (<NSIndexPath 0x7623700> 2 indexes [0, 1]); frame = (111.8 100; 50 50); 
2012-12-04 21:54:40.812 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76263c0> index path: (<NSIndexPath 0x76263a0> 2 indexes [0, 2]); frame = (173.6 100; 50 50); 
2012-12-04 21:54:40.813 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7623910> index path: (<NSIndexPath 0x7626440> 2 indexes [0, 3]); frame = (235.4 100; 50 50); 
2012-12-04 21:54:40.813 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76239a0> index path: (<NSIndexPath 0x7626460> 2 indexes [0, 4]); frame = (297.2 100; 50 50); 
2012-12-04 21:54:40.813 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7623ff0> index path: (<NSIndexPath 0x7623a40> 2 indexes [0, 5]); frame = (359 100; 50 50); 
2012-12-04 21:54:40.814 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76240a0> index path: (<NSIndexPath 0x7624070> 2 indexes [0, 6]); frame = (420.8 100; 50 50); 
2012-12-04 21:54:40.853 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7624150> index path: (<NSIndexPath 0x7624120> 2 indexes [0, 7]); frame = (482.6 100; 50 50); 
2012-12-04 21:54:40.853 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7624200> index path: (<NSIndexPath 0x76241d0> 2 indexes [0, 8]); frame = (544.4 100; 50 50); 
2012-12-04 21:54:40.853 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76242d0> index path: (<NSIndexPath 0x7623a20> 2 indexes [0, 9]); frame = (606.2 100; 50 50); 
2012-12-04 21:54:40.854 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7624380> index path: (<NSIndexPath 0x7624350> 2 indexes [0, 10]); frame = (668 100; 50 50); 
2012-12-04 21:54:40.854 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7624430> index path: (<NSIndexPath 0x7624400> 2 indexes [0, 11]); frame = (729.8 100; 50 50); 
2012-12-04 21:54:40.854 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76244e0> index path: (<NSIndexPath 0x76244b0> 2 indexes [0, 11]); frame = (50 160; 50 50); 
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x76427e0> index path: (<NSIndexPath 0xe1112f0> 2 indexes [0, 6]); frame = (420.8 100; 50 50); 
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7517f80> index path: (<NSIndexPath 0xe1113a0> 2 indexes [0, 7]); frame = (482.6 100; 50 50); 
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7149030> index path: (<NSIndexPath 0xe111450> 2 indexes [0, 8]); frame = (544.4 100; 50 50); 
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7149210> index path: (<NSIndexPath 0xe111230> 2 indexes [0, 9]); frame = (606.2 100; 50 50); 
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7518220> index path: (<NSIndexPath 0xe111020> 2 indexes [0, 2]); frame = (173.6 100; 50 50); 
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7518510> index path: (<NSIndexPath 0xe1110d0> 2 indexes [0, 3]); frame = (235.4 100; 50 50); 
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7518760> index path: (<NSIndexPath 0xe111180> 2 indexes [0, 4]); frame = (297.2 100; 50 50); 
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7518ac0> index path: (<NSIndexPath 0xe111250> 2 indexes [0, 5]); frame = (359 100; 50 50); 
2012-12-04 21:54:40.857 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7642bf0> index path: (<NSIndexPath 0xe110e00> 2 indexes [0, 0]); frame = (50 100; 50 50); 
2012-12-04 21:54:40.857 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7518d40> index path: (<NSIndexPath 0xe110f80> 2 indexes [0, 1]); frame = (111.8 100; 50 50); 
2012-12-04 21:54:40.857 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0xe111600> index path: (<NSIndexPath 0xe1115d0> 2 indexes [0, 10]); frame = (668 100; 50 50); 
2012-12-04 21:54:40.858 Animations[2079:c07] attributes: <UICollectionViewLayoutAttributes: 0x7642dd0> index path: (<NSIndexPath 0xe111680> 2 indexes [0, 11]); frame = (50 160; 50 50); 
2012-12-04 21:54:41.161 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76240a0> index path: (<NSIndexPath 0x7643bf0> 2 indexes [0, 0]); frame = (50 100; 50 50); 
2012-12-04 21:54:41.162 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7623ff0> index path: (<NSIndexPath 0x7644e90> 2 indexes [0, 1]); frame = (111.8 100; 50 50); 
2012-12-04 21:54:41.162 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76239a0> index path: (<NSIndexPath 0x7645310> 2 indexes [0, 2]); frame = (173.6 100; 50 50); 
2012-12-04 21:54:41.162 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7623910> index path: (<NSIndexPath 0x7642bb0> 2 indexes [0, 3]); frame = (235.4 100; 50 50); 
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76263c0> index path: (<NSIndexPath 0x761ab30> 2 indexes [0, 4]); frame = (297.2 100; 50 50); 
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76235e0> index path: (<NSIndexPath 0x7642170> 2 indexes [0, 5]); frame = (359 100; 50 50); 
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7611df0> index path: (<NSIndexPath 0x7642190> 2 indexes [0, 6]); frame = (420.8 100; 50 50); 
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7642070> index path: (<NSIndexPath 0x7642230> 2 indexes [0, 7]); frame = (482.6 100; 50 50); 
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7642ea0> index path: (<NSIndexPath 0x76422f0> 2 indexes [0, 8]); frame = (544.4 100; 50 50); 
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7645d00> index path: (<NSIndexPath 0x7641d30> 2 indexes [0, 9]); frame = (606.2 100; 50 50); 
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76469b0> index path: (<NSIndexPath 0x7642470> 2 indexes [0, 10]); frame = (668 100; 50 50); 
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7646cb0> index path: (<NSIndexPath 0x7642490> 2 indexes [0, 11]); frame = (729.8 100; 50 50); 
2012-12-04 21:54:41.165 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x76470b0> index path: (<NSIndexPath 0x7646d30> 2 indexes [0, 11]); frame = (50 160; 50 50); 
2012-12-04 21:54:41.165 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7517e40> index path: (<NSIndexPath 0x7517e10> 2 indexes [0, 0]); frame = (50 100; 50 50); 
2012-12-04 21:54:41.165 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7517f00> index path: (<NSIndexPath 0x7517ed0> 2 indexes [0, 1]); frame = (111.8 100; 50 50); 
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7517fb0> index path: (<NSIndexPath 0x7517f80> 2 indexes [0, 2]); frame = (173.6 100; 50 50); 
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7518060> index path: (<NSIndexPath 0x7518030> 2 indexes [0, 3]); frame = (235.4 100; 50 50); 
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7518110> index path: (<NSIndexPath 0x75180e0> 2 indexes [0, 4]); frame = (297.2 100; 50 50); 
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x75181d0> index path: (<NSIndexPath 0x75181b0> 2 indexes [0, 5]); frame = (359 100; 50 50); 
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7518280> index path: (<NSIndexPath 0x7518250> 2 indexes [0, 6]); frame = (420.8 100; 50 50); 
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7518330> index path: (<NSIndexPath 0x7518300> 2 indexes [0, 7]); frame = (482.6 100; 50 50); 
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x75183e0> index path: (<NSIndexPath 0x75183b0> 2 indexes [0, 8]); frame = (544.4 100; 50 50); 
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x75184b0> index path: (<NSIndexPath 0x7518190> 2 indexes [0, 9]); frame = (606.2 100; 50 50); 
2012-12-04 21:54:41.168 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7518560> index path: (<NSIndexPath 0x7518530> 2 indexes [0, 10]); frame = (668 100; 50 50); 
2012-12-04 21:54:41.168 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x7518610> index path: (<NSIndexPath 0x75185e0> 2 indexes [0, 11]); frame = (729.8 100; 50 50); 
2012-12-04 21:54:41.168 Animations[2079:c07] cellAttributes: <UICollectionViewLayoutAttributes: 0x75186c0> index path: (<NSIndexPath 0x7518690> 2 indexes [0, 11]); frame = (50 160; 50 50); 
2012-12-04 21:54:40.808动画[2079:c07]属性:索引路径:(2个索引[0,4]);帧=(297.2100;50);
2012-12-04 21:54:40.809动画[2079:c07]单元属性:索引路径:(2个索引[0,0]);帧=(50 100;50 50);
2012-12-04 21:54:40.812动画[2079:c07]单元属性:索引路径:(2个索引[0,1]);帧=(111.8100;50);
2012-12-04 21:54:40.812动画[2079:c07]单元属性:索引路径:(2个索引[0,2]);帧=(173.6100;50);
2012-12-04 21:54:40.813动画[2079:c07]单元属性:索引路径:(2个索引[0,3]);帧=(235.4100;50);
2012-12-04 21:54:40.813动画[2079:c07]单元属性:索引路径:(2个索引[0,4]);帧=(297.2100;50);
2012-12-04 21:54:40.813动画[2079:c07]单元属性:索引路径:(2个索引[0,5]);帧=(359100;50);
2012-12-04 21:54:40.814动画[2079:c07]单元属性:索引路径:(2个索引[0,6]);帧=(420.8100;50);
2012-12-04 21:54:40.853动画[2079:c07]单元属性:索引路径:(2个索引[0,7]);帧=(482.6100;50);
2012-12-04 21:54:40.853动画[2079:c07]单元属性:索引路径:(2个索引[0,8]);帧=(544.4100;50);
2012-12-04 21:54:40.853动画[2079:c07]单元属性:索引路径:(2个索引[0,9]);帧=(606.2100;50);
2012-12-04 21:54:40.854动画[2079:c07]单元属性:索引路径:(2个索引[0,10]);帧=(668100;50);
2012-12-04 21:54:40.854动画[2079:c07]单元属性:索引路径:(2个索引[0,11]);帧=(729.8100;50);
2012-12-04 21:54:40.854动画[2079:c07]单元属性:索引路径:(2个索引[0,11]);帧=(50 160;50 50);
2012-12-04 21:54:40.855动画[2079:c07]属性:索引路径:(2个索引[0,6]);帧=(420.8100;50);
2012-12-04 21:54:40.855动画[2079:c07]属性:索引路径:(2个索引[0,7]);帧=(482.6100;50);
2012-12-04 21:54:40.855动画[2079:c07]属性:索引路径:(2个索引[0,8]);帧=(544.4100;50);
2012-12-04 21:54:40.855动画[2079:c07]属性:索引路径:(2个索引[0,9]);帧=(606.2100;50);
2012-12-04 21:54:40.856动画[2079:c07]属性:索引路径:(2个索引[0,2]);帧=(173.6100;50);
2012-12-04 21:54:40.856动画[2079:c07]属性:索引路径:(2个索引[0,3]);帧=(235.4100;50);
2012-12-04 21:54:40.856动画[2079:c07]属性:索引路径:(2个索引[0,4]);帧=(297.2100;50);
2012-12-04 21:54:40.856动画[2079:c07]属性:索引路径:(2个索引[0,5]);帧=(359100;50);
2012-12-04 21:54:40.857动画[2079:c07]属性:索引路径:(2个索引[0,0]);帧=(50 100;50 50);
2012-12-04 21:54:40.857动画[2079:c07]属性:索引路径:(2个索引[0,1]);帧=(111.8100;50);
2012-12-04 21:54:40.857动画[2079:c07]属性:索引路径:(2个索引[0,10]);帧=(668100;50);
2012-12-04 21:54:40.858动画[2079:c07]属性:索引路径:(2个索引[0,11]);帧=(50 160;50 50);
2012-12-04 21:54:41.161动画[2079:c07]单元属性:索引路径:(2个索引[0,0]);帧=(50 100;50 50);
2012-12-04 21:54:41.162动画[2079:c07]单元属性:索引路径:(2个索引[0,1]);帧=(111.8100;50);
2012-12-04 21:54:41.162动画[2079:c07]单元属性:索引路径:(2个索引[0,2]);帧=(173.6100;50);
2012-12-04 21:54:41.162动画[2079:c07]单元属性:索引路径:(2个索引[0,3]);帧=(235.4100;50);
2012-12-04 21:54:41.163动画[2079:c07]单元属性:索引路径:(2个索引[0,4]);帧=(297.2100;50);
2012-12-04 21:54:41.163动画[2079:c07]单元属性:索引路径:(2个索引[0,5]);帧=(359100;50);
2012-12-04 21:54:41.163动画[2079:c07]单元属性:索引路径:(2个索引[0,6]);帧=(420.8100;50);
2012-12-04 21:54:41.163动画[2079:c07]单元属性:索引路径:(2个索引[0,7]);帧=(482.6100;50);
2012-12-04 21:54:41.164动画[2079:c07]单元属性:索引路径:(2个索引[0,8]);帧=(544.4100;50);
2012-12-04 21:54:41.164动画[2079:c07]单元属性:索引路径:(2个索引[0,9]);帧=(606.2100;50);
2012-12-04 21:54:41.164动画[2079:c07]单元属性:索引路径:(2个索引[0,10]);帧=(668100;50);
2012-12-04 21:54:41.164动画[2079:c07]单元属性:索引路径:(2个索引[0,11]);帧=(729.8100;50);
2012-12-04 21:54:41.165动画[2079:c07]单元属性:索引路径:(2个索引[0,11]);帧=(50 160;50 50);
2012-12-04 21:54:41.165动画
- (IBAction)switchLayout:(id)sender {
    CGPoint originalContentOffset = self.collectionView.contentOffset;
    PinchLayout *pinchLayout = [[PinchLayout alloc] init];
    [self.collectionView setCollectionViewLayout:pinchLayout animated:NO];
    self.collectionView.contentOffset = originalContentOffset;
}