Установка нового collectionViewLayout для UICollectionView с пустым экраном
Я начинаю экспериментировать с пользовательскими макетами UICollectionView. У меня есть UICollectionView в моем UIViewController, который соответствует протоколам UICollectionViewDataSource и UICollectionViewFlowLayout. Каждая ячейка - это просто изображение и метка. У меня есть кнопка, которая делает это:
- (IBAction)switchLayout:(id)sender {
PinchLayout *pinchLayout = [[PinchLayout alloc] init];
[self.collectionView setCollectionViewLayout:pinchLayout];
}
PinchLayout копируется прямо из Apple 'Пример кода. Когда вызывается метод switchLayout: мой collectionView исчезает. Я нене понимаю почему. Я думал, что это оживит новые позиции. Или в этом случае не делайте ничего, кроме добавления поддержки жестов для нового макета. Затем, когда я снова нажимаю на кнопку, я вижу новый макет, где все мои ячейки представления коллекции сокращены до версий их первоначального размера. Вот's PinchLayout.m
-(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: я зарегистрировал атрибуты и cellAttributes, и похоже, что кадры всегда находятся в границах экрана, поэтому яЯ до сих пор не уверен, почему клетки уходят с экрана наверх. Вот'Журнал: я
2012-12-04 21:54:40.808 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 4]); frame = (297.2 100; 50 50);
2012-12-04 21:54:40.809 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 0]); frame = (50 100; 50 50);
2012-12-04 21:54:40.812 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 1]); frame = (111.8 100; 50 50);
2012-12-04 21:54:40.812 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 2]); frame = (173.6 100; 50 50);
2012-12-04 21:54:40.813 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 3]); frame = (235.4 100; 50 50);
2012-12-04 21:54:40.813 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 4]); frame = (297.2 100; 50 50);
2012-12-04 21:54:40.813 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 5]); frame = (359 100; 50 50);
2012-12-04 21:54:40.814 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 6]); frame = (420.8 100; 50 50);
2012-12-04 21:54:40.853 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 7]); frame = (482.6 100; 50 50);
2012-12-04 21:54:40.853 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 8]); frame = (544.4 100; 50 50);
2012-12-04 21:54:40.853 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 9]); frame = (606.2 100; 50 50);
2012-12-04 21:54:40.854 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 10]); frame = (668 100; 50 50);
2012-12-04 21:54:40.854 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 11]); frame = (729.8 100; 50 50);
2012-12-04 21:54:40.854 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 11]); frame = (50 160; 50 50);
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 6]); frame = (420.8 100; 50 50);
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 7]); frame = (482.6 100; 50 50);
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 8]); frame = (544.4 100; 50 50);
2012-12-04 21:54:40.855 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 9]); frame = (606.2 100; 50 50);
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 2]); frame = (173.6 100; 50 50);
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 3]); frame = (235.4 100; 50 50);
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 4]); frame = (297.2 100; 50 50);
2012-12-04 21:54:40.856 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 5]); frame = (359 100; 50 50);
2012-12-04 21:54:40.857 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 0]); frame = (50 100; 50 50);
2012-12-04 21:54:40.857 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 1]); frame = (111.8 100; 50 50);
2012-12-04 21:54:40.857 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 10]); frame = (668 100; 50 50);
2012-12-04 21:54:40.858 Animations[2079:c07] attributes: index path: ( 2 indexes [0, 11]); frame = (50 160; 50 50);
2012-12-04 21:54:41.161 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 0]); frame = (50 100; 50 50);
2012-12-04 21:54:41.162 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 1]); frame = (111.8 100; 50 50);
2012-12-04 21:54:41.162 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 2]); frame = (173.6 100; 50 50);
2012-12-04 21:54:41.162 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 3]); frame = (235.4 100; 50 50);
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 4]); frame = (297.2 100; 50 50);
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 5]); frame = (359 100; 50 50);
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 6]); frame = (420.8 100; 50 50);
2012-12-04 21:54:41.163 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 7]); frame = (482.6 100; 50 50);
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 8]); frame = (544.4 100; 50 50);
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 9]); frame = (606.2 100; 50 50);
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 10]); frame = (668 100; 50 50);
2012-12-04 21:54:41.164 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 11]); frame = (729.8 100; 50 50);
2012-12-04 21:54:41.165 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 11]); frame = (50 160; 50 50);
2012-12-04 21:54:41.165 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 0]); frame = (50 100; 50 50);
2012-12-04 21:54:41.165 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 1]); frame = (111.8 100; 50 50);
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 2]); frame = (173.6 100; 50 50);
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 3]); frame = (235.4 100; 50 50);
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 4]); frame = (297.2 100; 50 50);
2012-12-04 21:54:41.166 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 5]); frame = (359 100; 50 50);
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 6]); frame = (420.8 100; 50 50);
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 7]); frame = (482.6 100; 50 50);
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 8]); frame = (544.4 100; 50 50);
2012-12-04 21:54:41.167 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 9]); frame = (606.2 100; 50 50);
2012-12-04 21:54:41.168 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 10]); frame = (668 100; 50 50);
2012-12-04 21:54:41.168 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 11]); frame = (729.8 100; 50 50);
2012-12-04 21:54:41.168 Animations[2079:c07] cellAttributes: index path: ( 2 indexes [0, 11]); frame = (50 160; 50 50);