【问题标题】:swift positionate center UICollectionViewCell快速定位中心 UICollectionViewCell
【发布时间】:2015-04-18 17:48:51
【问题描述】:

我正在尝试实现“snap to effect”,但无法正常工作,是不是我做错了什么?

继承 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)
}

Sample project heresnap effect, desired output

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

    我终于知道了

    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)
    }
    

    别忘了设置委托:

    class MyCostomFlow: UICollectionViewFlowLayout, UICollectionViewDelegateFlowLayout {
    
        var delegate:MyFlowLayoutDelegate?
    }
    

    以及委托本身:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-09
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多