CollectionView:如何使位于视图中心的像元尺寸更大?

时间:2019-01-28 13:21:33

标签: ios objective-c uicollectionview collectionview

我想创建一个像this image这样的设计。这是一个Camera UI,用户可以在录制视频时滚动浏览不同的面罩并应用任何面罩。

我浏览了许多网址,例如:

Swift How to get the centre cell in a collectionView and resize it when scrolling?

How to make center image bigger when scrolled horizondally?

但是这些解决方案都不适合我。

这是我的代码:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGPoint point = CGPointMake(self.view.frame.size.width/2.0,40);
        if([collectionView indexPathForItemAtPoint:point] != nil)
        {
            NSIndexPath *centerIndex = [collectionView indexPathForItemAtPoint:point];
            if(centerIndex != nil)
            {
                if (indexPath.item == centerIndex.item) {
                    return CGSizeMake(100, 100);
                }
            }
        }
    return CGSizeMake(80, 80);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //top, left, bottom, right
    return UIEdgeInsetsMake(0, 16, 0, 16);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FavImageCollectionViewCell *newCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FavImageCollectionViewCell" forIndexPath:indexPath];
    if (!newCell)
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"FavImageCollectionViewCell" owner:self options:nil];
        newCell = [cellArray objectAtIndex:0];
    }

    newCell.contentView.backgroundColor = [UIColor redColor];

    return newCell;
}

但是在我的代码中,应用程序在

崩溃
  

[collectionView indexPathForItemAtPoint:point]

它给出如下错误:

  

由于未捕获的异常“ NSRangeException”而终止应用程序,原因:   '***-[__ NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]'

我非常感谢我对代码的任何建议或改进。

0 个答案:

没有答案