Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.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定位中心UICollectionViewCell_Ios_Swift_Uicollectionview_Uicollectionviewcell_Uicollectionviewlayout - Fatal编程技术网

Ios swift定位中心UICollectionViewCell

Ios swift定位中心UICollectionViewCell,ios,swift,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我正在尝试实现“快速生效”,但无法使其工作,是否有什么地方我做错了 子类化UICollectionViewFlowLayout并重写targetContentOffsetForProposedContentOffset函数: override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) ->

我正在尝试实现“快速生效”,但无法使其工作,是否有什么地方我做错了

子类化
UICollectionViewFlowLayout
并重写
targetContentOffsetForProposedContentOffset
函数:

override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

    var offsetAdjustment:CGFloat = CGFloat(MAXFLOAT)
    let verticalCenter:CGFloat = proposedContentOffset.y + (CGRectGetHeight(self.collectionView!.bounds) / 2.0)
    let proposedRect:CGRect = CGRectMake(proposedContentOffset.x,0.0,self.collectionView!.bounds.size.width,self.collectionView!.bounds.size.height)

    let array:NSArray = self.layoutAttributesForElementsInRect(proposedRect)!

    for layoutAttributes : AnyObject in array {

        if let _layoutAttributes = layoutAttributes as? UICollectionViewLayoutAttributes {

            if _layoutAttributes.representedElementCategory != UICollectionElementCategory.Cell {
                continue
            }

            let itemVerticalCenter:CGFloat = layoutAttributes.center.y

            let _verticalCenter = fabsf(Float(itemVerticalCenter) - Float(verticalCenter))
            let _offsetAdjustment = fabsf(Float(offsetAdjustment))

            if (_verticalCenter < _offsetAdjustment) {
                offsetAdjustment = itemVerticalCenter - verticalCenter
            }
        }
    }
    return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y)
}
覆盖func targetContentOffsetForProposedContentOffset(proposedContentOffset:CGPoint,使用CrollingVelocity:CGPoint)->CGPoint{
var补偿调整:CGFloat=CGFloat(MAXFLOAT)
让verticalCenter:CGFloat=proposedContentOffset.y+(CGRectGetHeight(self.collectionView!.bounds)/2.0)
让proposedRect:CGRect=CGRectMake(proposedContentOffset.x,0.0,self.collectionView!.bounds.size.width,self.collectionView!.bounds.size.height)
让数组:NSArray=self.layouttributesforementsinrect(proposedRect)!
对于LayoutAttribute:数组中的任意对象{
如果让_layouttributes=layouttributes作为UICollectionViewLayouttributes{
如果_layouttributes.representedElementCategory!=UICollectionElementCategory.Cell{
持续
}
让itemVerticalCenter:CGFloat=layouttributes.center.y
设_verticalCenter=fabsf(Float(itemVerticalCenter)-Float(verticalCenter))
设_offsetAdjustment=fabsf(Float(offsetAdjustment))
如果(_垂直中心<_偏移调整){
偏移调整=项目垂直中心-垂直中心
}
}
}
返回CGPointMake(proposedContentOffset.x+offsetAdjustment,proposedContentOffset.y)
}

最后我终于找到了答案

override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

    let rectBounds:CGRect = self.collectionView!.bounds
    let halfHeight:CGFloat = rectBounds.size.height * CGFloat(0.45)
    let proposedContentOffsetCenterY:CGFloat = proposedContentOffset.y + halfHeight

    let attributesArray:NSArray = self.layoutAttributesForElementsInRect(rectBounds)!

    var candidateAttributes:UICollectionViewLayoutAttributes?

    for layoutAttributes : AnyObject in attributesArray {

        if let _layoutAttributes = layoutAttributes as? UICollectionViewLayoutAttributes {

            if _layoutAttributes.representedElementCategory != UICollectionElementCategory.Cell {
                continue
            }

            if candidateAttributes == nil {
                candidateAttributes = _layoutAttributes
                self.delegate?.cellCenteredAtIndexPathWithCollectionView(self.collectionView!, layout: self, indexPath: candidateAttributes!.indexPath)
                continue
            }

            if fabsf(Float(_layoutAttributes.center.y) - Float(proposedContentOffsetCenterY)) < fabsf(Float(candidateAttributes!.center.y) - Float(proposedContentOffsetCenterY)) {
                candidateAttributes = _layoutAttributes
                self.delegate?.cellCenteredAtIndexPathWithCollectionView(self.collectionView!, layout: self, indexPath: candidateAttributes!.indexPath)
            }
        }
    }

    if attributesArray.count == 0 {
        return CGPointMake(proposedContentOffset.x,proposedContentOffset.y - halfHeight * 2)
    }

    return CGPointMake(proposedContentOffset.x,candidateAttributes!.center.y - halfHeight)
}
代表本人:

protocol MyFlowLayoutDelegate
{
    func cellCenteredAtIndexPathWithCollectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, indexPath: NSIndexPath) -> Void
}
protocol MyFlowLayoutDelegate
{
    func cellCenteredAtIndexPathWithCollectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, indexPath: NSIndexPath) -> Void
}