水平UICollectionView单行布局

Mic*_*969 5 objective-c horizontalscrollview ios uicollectionview uicollectionviewlayout

我正在尝试设置什么应该是一个简单的水平布局使用UICollectionView,绕圈而没有达到预期的结果所以任何指针或例子将不胜感激.

可能很少有点我粘贴代码经常改变而没有成功.

图像显示两行,第一行是单个项目,大小正确,并在中心正确对齐.第二行有3个项目,第一行应该与上面的项目大小相同,下一个项目边缘只是可见,表示另一个项目.当项目向左滑动时,它们应该是3个可见的项目 - 主要项目在中心完全可见,项目1和3在左右边缘可见.

我曾尝试设置分页上,不断变化的insetForSectionAtIndex,minimumLineSpacingForSectionAtIndex,minimumInteritemSpacingForSectionAtIndex.


编辑 - 12月16日

我没有解释或说明这一点,所以下面有更好的说明.I know how to create the collection view, set the item size etc but can not get the desired layout.

这是单行水平UICollectionView,其中一个项目始终在全视图中,并且其部分视图中的相邻项目向用户指示根据滚动位置和项目计数向左或向右的项目.

  • 当项目1处于全视图时,项目2处于局部视图中
  • 当项目2处于全视图时,项目1和3处于局部视图(左和右)
  • 当第3项处于全视图时,第2项处于局部视图(左)

Arp*_*ane 1

创建一个集合视图并根据第二行中项目的高度设置高度,然后将滚动方向设置为水平。还

这是附加的图像,请相应地检查集合视图的设置。

在此输入图像描述

以下方法将根据第二行设置单元格宽度。第一个单元格将显示在屏幕的 3/4 部分,第二个单元格将显示在屏幕的 1/4 部分。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

return CGSizeMake( (self.view.frame.size.width / 4) * 3, "YOUR SECOND ROW ITEM HEIGHT");
Run Code Online (Sandbox Code Playgroud)

}

我已经对屏幕上的 5 个单元格执行了此操作,并附加了下面的代码。

在此输入图像描述

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake( self.view.frame.size.width / 5, 70);

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row > pos)
    {
        if (indexPath.row - pos == 2) {
            pos = pos + 1;
        }
        [self changeDate];
    }
    else if (indexPath.row == pos)
    {
        NSLog(@"Do Nothing");
    }
    else
    {
        if (indexPath.row + 2 == pos) {
            pos = pos - 1;
        }
        [self changeDate1];
    }
    //NSLog(@"%@",[arrDate objectAtIndex:indexPath.row]);
}


-(void)changeDate
{
    if (pos<(arrDate.count - 2)) {
        pos=pos+1;
        [self.tpCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:pos inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
        NSLog(@"%@",[arrDate objectAtIndex:pos]);
        self.lblMovieName.text =[arrDate objectAtIndex:pos];
    }
}
-(void)changeDate1
{
    if(pos>2)
    {
        pos=pos-1;
        [self.tpCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:pos inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
        NSLog(@"%@",[arrDate objectAtIndex:pos]);
        self.lblMovieName.text =[arrDate objectAtIndex:pos];
    }
    else{

    }
}
Run Code Online (Sandbox Code Playgroud)