Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Swift 3:UICollectionView水平中心和更大的单元格_Ios_Swift_Xcode_Uicollectionview_Uicollectionviewlayout - Fatal编程技术网

iOS Swift 3:UICollectionView水平中心和更大的单元格

iOS Swift 3:UICollectionView水平中心和更大的单元格,ios,swift,xcode,uicollectionview,uicollectionviewlayout,Ios,Swift,Xcode,Uicollectionview,Uicollectionviewlayout,我想构建一个集合视图,如下所示: 在中心有较大的单元,单元被捕捉到容器视图的中心,但使用Swift 3。我不想使用库,因为我想学习如何构建这样的自定义集合视图 我已经搜索了这么多,但还没有找到任何合适的解决方案编写该函数 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: Inde

我想构建一个集合视图,如下所示:

在中心有较大的单元,单元被捕捉到容器视图的中心,但使用Swift 3。我不想使用库,因为我想学习如何构建这样的自定义集合视图

我已经搜索了这么多,但还没有找到任何合适的解决方案

编写该函数

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
    }
使集合视图[滚动方向]水平

和[滚动]勾选滚动启用和分页启用

使细胞变大

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let offSet = self.collectionView.contentOffset
    let width = self.collectionView.bounds.size.width

    let index = round(offSet.x / width)
    let newPoint = CGPoint(x: index * size.width, y: offSet.y)


    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in

    },completion: {(UIVIewTransitionCoordinatorContext) in
        self.collectionView.reloadData()
        self.collectionView.setContentOffset(newPoint, animated: true)
    })

}

要实现这一点,您需要子类化
UICollectionViewFlowLayout
并覆盖:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
然后打电话给super.layoutAt。。。并更改它通过.transform属性返回的单元格,然后返回更改后的属性

这是我之前做的一个例子

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

var att = super.layoutAttributesForElements(in: rect)!

if !(delegate?.reelPlayerFlowIsPreviewMode() ?? true) {
    return att
}
let region = CGRect(x: (self.collectionView?.contentOffset.x)!,
                    y: (self.collectionView?.contentOffset.y)!,
                    width: (self.collectionView?.bounds.size.width)!,
                    height: (self.collectionView?.bounds.size.height)!)

let center = CGPoint(x: region.midX, y: region.midY)

for theCell in att {

    print("\(theCell.indexPath.item)\n\(theCell)\n")
    let cell = theCell.copy() as! UICollectionViewLayoutAttributes
    var f = cell.frame
    let cellCenter = CGPoint(x: f.midX, y: f.midY)
    let realDistance = min(center.x - cellCenter.x, region.width)
    let distance = abs(realDistance)
    let d = (region.width - distance) / region.width
    let p = (max(d, ReelPlayerFlowLayout.minPercent) * ReelPlayerFlowLayout.maxPercent)

    f.origin.x += (realDistance * ((1 - ReelPlayerFlowLayout.maxPercent) + (ReelPlayerFlowLayout.maxPercent - ReelPlayerFlowLayout.minPercent)))

    cell.frame = f
    cell.size = CGSize (width: f.width * p, height: f.height * p)            
    let index = att.index(of: theCell)!
    att[index] = cell
}

return att

}

您好,请查看链接,如果有帮助,请投票。这一个很好用,我怎么能接受这个答案呢?只需按下国旗上方的按钮,向上投票,你们可以通过将指针悬停在我的答案上找到两者。谢谢。如果ans是正确的,那么标记为写ans。如何使中心单元格比左右单元格大?