HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UICollectionView具有3个项目的水平分页大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在UICollectionView中显示3个项目,像这样启用分页

但我正在变得这样

我已经做了自定义流程,加上分页被启用,但不能得到我需要的。我该如何实现这个或哪个代表我应该研究,或指导我一些链接,从那里我可以得到这种情况的帮助。

- (void)awakeFromNib
{
    self.itemSize = CGSizeMake(480,626);
    self.minimumInteritemSpacing = 112;
    self.minimumLineSpacing = 112;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.sectionInset = UIEdgeInsetsMake(0,272,272);
}

解决方法

编辑:
演示链接https://github.com/raheelsadiq/UICollectionView-horizontal-paging-with-3-items

经过大量的搜索后,我找到了,找到下一个点来滚动并禁用分页。在scrollviewWillEndDragging滚动到下一个单元格x。

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVeLocity:(CGPoint)veLocity targetContentOffset:(inout CGPoint *)targetContentOffset
{

    float pageWidth = 480 + 50; // width + space

    float currentOffset = scrollView.contentOffset.x;
    float targetOffset = targetContentOffset->x;
    float newTargetOffset = 0;

    if (targetOffset > currentOffset)
        newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth;
    else
        newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth;

    if (newTargetOffset < 0)
        newTargetOffset = 0;
    else if (newTargetOffset > scrollView.contentSize.width)
        newTargetOffset = scrollView.contentSize.width;

    targetContentOffset->x = currentOffset;
    [scrollView setContentOffset:CGPointMake(newTargetOffset,scrollView.contentOffset.y) animated:YES];
}

我也不得不使左右小中心变大,所以我做了变形。
问题是找到索引,所以很难找到。

对于在这方法中左右的变换使用newTargetOffset

int index = newTargetOffset / pageWidth;

if (index == 0) { // If first index 
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index  inSection:0]];

    [UIView animateWithDuration:ANIMATION_SPEED animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];
    cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index + 1  inSection:0]];
    [UIView animateWithDuration:ANIMATION_SPEED animations:^{
        cell.transform = TRANSFORM_CELL_VALUE;
    }];
}else{
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    [UIView animateWithDuration:ANIMATION_SPEED animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];

    index --; // left
    cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    [UIView animateWithDuration:ANIMATION_SPEED animations:^{
        cell.transform = TRANSFORM_CELL_VALUE;
    }];

    index ++;
    index ++; // right
    cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    [UIView animateWithDuration:ANIMATION_SPEED animations:^{
        cell.transform = TRANSFORM_CELL_VALUE;
    }];
}

并在cellForRowAtIndex中添加

if (indexPath.row == 0 && isfirstTimeTransform) { // make a bool and set YES initially,this check will prevent fist load transform
    isfirstTimeTransform = NO;
}else{
    cell.transform = TRANSFORM_CELL_VALUE; // the new cell will always be transform and without animation 
}

也可以添加这两个宏,或者您希望处理这两个宏

#define TRANSFORM_CELL_VALUE CGAffineTransformMakeScale(0.8,0.8)
#define ANIMATION_SPEED 0.2

最终的结果是

大佬总结

以上是大佬教程为你收集整理的ios – UICollectionView具有3个项目的水平分页全部内容,希望文章能够帮你解决ios – UICollectionView具有3个项目的水平分页所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: