0

我有一个ViewController mycollectionview并在其中创建一个UICollectionView collection view并将大小设置为帧大小。我有一个自定义UICollectionViewCell CollectionViewCell类。图像大小不同,我想同时显示一个单元格(不滚动时)。 这里是mycollectionview代码:UICollectionView垂直分页与代码目标-C

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    imgnum = @[@"1.jpg",@"2.jpg",@"3.jpeg",@"4.jpg"]; 
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; 
    layout.itemSize = CGSizeMake(80.0, 80.0); 
    collectionview = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; 
    collectionview.translatesAutoresizingMaskIntoConstraints = NO; 
    collectionview.delegate = self; 
    collectionview.dataSource = self; 
    collectionview.bounces = false; 
    [collectionview registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 
    [self.view addSubview:collectionview]; 
{ 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CollectionViewCell *cell = [collectionview dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    cell.pic.image = [UIImage imageNamed:imgnum[indexPath.row%4]]; 
    cell.lab.text = @"Eiffel"; 
    return cell; 
} 

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    float w = self.view.frame.size.width; 
    CGSize a = [UIImage imageNamed:imgnum[indexPath.row%4]].size; 
    float aspect = a.width/a.height; 
    return CGSizeMake(w, w * 1/aspect); 
} 

它是我CollectionViewCell代码:

- (void)initialize 
{ 
    _pic = [UIImageView new]; 
    _pic.translatesAutoresizingMaskIntoConstraints=false; 
    [self.contentView addSubview:_pic]; 
    _lab = [UILabel new]; 
    _lab.textColor = [UIColor blackColor]; 
    _lab.translatesAutoresizingMaskIntoConstraints = false; 
    [self.contentView addSubview:_lab]; 
} 
+0

我会推荐你​​不要使用这个CGSize a = [UIImage imageNamed:imgnum [indexPath.row%4]]。size;作为图像大小,如果超过设备框架高度,将会产生问题并将该单元展开为可滚动。您希望一次显示一个图像,因此我可以将contentcell高度设置为与设备高度或collectionview的框架相同。稍后设置UIImageview setContentMode:AspectFit,以便图像适合一个单元格,并且在一段时间内可以适用于一个图元的滚动。 –

回答

0

我recommned你不要用这个CGSize A = [UIImage的imageNamed:imgnum [indexPath.row%4] 。尺寸;作为图像大小,如果超过设备框架高度,将会产生问题并将该单元展开为可滚动。

  1. 您希望一次显示一个图像,所以我会说设置contentcell高度与设备高度或collectionview的框架相同。
  2. 稍后设置UIImageview setContentMode:AspectFit,以便图像适合一个单元格,并且您在一次的图像上滚动时可以工作。

查看我为您创建的示例,希望它有所帮助。 http://www.filedropper.com/collectionviewperpageex

+0

感谢您的帮助。在这种类型的收集视图,你说,如果释放滚动2图像之间,屏幕停止在2帧图像不是1图像。我想要它显示最近的图像。 –

+0

然后仅使用页面控件和滚动视图,或者手动将滚动插入移动到下一页矩形。在cocacontrols.com网站上查找MediaGallery原型 –