简体   繁体   English

UICollectionView自定义水平分页布局

[英]UICollectionView custom horizontal paging layout

[![collectionview example][1]][1] [![collectionview示例] [1]] [1]

I am trying to emulate this behavior using a collection view. 我正在尝试使用集合视图来模拟这种行为。 I have started by working with this method and it has gotten me closer. 我从使用这种方法开始,它使我更加接近。 Although as I swipe further and further right on my horizontal paging CollectionView, the content shifts further and further left. 尽管在水平分页CollectionView上向右滑动时,内容向左移。 Also the spacing between the cells is off. 单元之间的间距也关闭。 I believe it requires a custom layout, but am not sure. 我相信它需要自定义布局,但不确定。

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    return CGSize(width: collectionView.frame.width - 20, height: collectionView.frame.height - 20)

}

it depends on 3 factors 1) Section Insets 2) Cell Spacing 3) Cell Size 它取决于3个因素1)截面插图2)单元间距3)单元大小

Any change in each you have to change others for your case 每个方面的任何更改都必须针对您的情况更改其他

1) Set left & right with 20 1)左右设置20

在此处输入图片说明

2) set cell spacing to 10 2)将像元间距设置为10

func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 10
}
func collectionView(collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
    minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 10
}

3) Set cell size 3)设置像元大小

func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width / 1.15 ,height: collectionView.frame.height)
}

4) this will center cell in screen 4)这将使屏幕中的单元格居中

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    let pageWidth:CGFloat = scrollView.frame.width / 1.15 + cellSpacing ;

    let currentOffset:CGFloat = scrollView.contentOffset.x;
    let targetOffset:CGFloat = targetContentOffset.memory.x
    var newTargetOffset:CGFloat = 0;

    if targetOffset > currentOffset
    {
        newTargetOffset = ceil(currentOffset / pageWidth) * pageWidth;
    }
    else
    {
        newTargetOffset = floor(currentOffset / pageWidth) * pageWidth;
    }

    if newTargetOffset < 0
    {
        newTargetOffset = 0
    }
    else if newTargetOffset > scrollView.contentSize.width
    {
    newTargetOffset = scrollView.contentSize.width;
    }

    targetContentOffset.memory.x = currentOffset

    scrollView.setContentOffset(CGPointMake(newTargetOffset, 0), animated: true)

}

I've changed a bit @Mohamed's solution and it worked perfectly for me: 我对@Mohamed的解决方案进行了一些更改,它对我来说非常有效:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    let cellSpacing: CGFloat = self.collectionView(self.collectionView, layout: self.collectionView.collectionViewLayout, minimumInteritemSpacingForSectionAt: 0)
    let pageWidth: CGFloat = self.pageWidth + cellSpacing

    let currentOffset = scrollView.contentOffset.x
    let targetOffset = targetContentOffset.pointee.x
    targetContentOffset.pointee.x = currentOffset

    var pageNumber = 0
    if targetOffset > currentOffset {
        pageNumber = Int(ceil(currentOffset / pageWidth))
    }
    else {
        pageNumber = Int(floor(currentOffset / pageWidth))
    }

    let indexPath = IndexPath(row: pageNumber, section: 0)
    self.collectionView.scrollToItem(at: indexPath, at: UICollectionView.ScrollPosition.centeredHorizontally, animated: true)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM