HorizontalStickyHeaderLayout 0.5.2

HorizontalStickyHeaderLayout 0.5.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2020年12月
SPM支持 SPM

Toshihiro Suzuki 维护。



HorizontalStickyHeaderLayout

带有粘性HeaderView的水平UICollectionViewLayout

platforms Carthage compatible Cocoapods pod License

需求

  • iOS9+
  • tvOS9+

如何使用

只需实现这5个必需的代理方法。

extension ViewController: HorizontalStickyHeaderLayoutDelegate {
    private enum Const {
        static let headerSize = CGSize(width: 100, height: 38)
        static let itemSize0  = CGSize(width: 50, height: 50)
        static let itemSize1  = CGSize(width: 80, height: 80)
        static let headerLeft: CGFloat = 8
    }
    func collectionView(_ collectionView: UICollectionView, hshlSizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
        if indexPath.section % 2 == 0 {
            return Const.itemSize0
        } else {
            return Const.itemSize1
        }
    }
    func collectionView(_ collectionView: UICollectionView, hshlSizeForHeaderAtSection section: Int) -> CGSize {
        return Const.headerSize
    }
    func collectionView(_ collectionView: UICollectionView, hshlHeaderInsetsAtSection section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: Const.headerLeft, bottom: 20, right: 20)
    }
    func collectionView(_ collectionView: UICollectionView, hshlMinSpacingForCellsAtSection section: Int) -> CGFloat {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, hshlSectionInsetsAtSection section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: section == 4 ? 0 : 20)
    }
}

可选地,您可以定义外边距的 contentInset

请参阅示例以获取详细信息。

tvOS免费提供动画标题Y位置!

如何实现

  • 在获得焦点时,调用 updatePoppingHeaderIndexPaths() 来重新计算弹出标题的 indexPath 以获取最新的 indexPath。
  • 通过实现 collectionView(_:,hshlDidUpdatePoppingHeaderIndexPaths:) 代理方法来监听滚动时的 pop indexPath 变化。
  • 为你的头部视图的容器视图进行动画。

查看示例以了解推荐的实现方式。

    // Either in UICollectionViewDelegate or this override method.
    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        layout.updatePoppingHeaderIndexPaths()
        let (pop, unpop) = self.getHeaders(poppingHeadersIndexPaths: self.layout.poppingHeaderIndexPaths)
        UIView.animate(withDuration: Const.unpopDuration, delay: 0, options: [.curveEaseOut], animations: {
            unpop.forEach { $0.unpopHeader() }
        }, completion: nil)
        coordinator.addCoordinatedAnimations({
            pop.forEach { $0.popHeader() }
        }, completion: nil)
        super.didUpdateFocus(in: context, with: coordinator)
    }

    func collectionView(_ collectionView: UICollectionView, hshlDidUpdatePoppingHeaderIndexPaths indexPaths: [IndexPath]) {
        let (pop, unpop) = self.getHeaders(poppingHeadersIndexPaths: self.layout.poppingHeaderIndexPaths)
        UIView.animate(withDuration: Const.unpopDuration, delay: 0, options: [.curveEaseOut], animations: {
            unpop.forEach { $0.unpopHeader() }
            pop.forEach { $0.popHeader() }
        }, completion: nil)
    }

安装

Carthage

github "toshi0383/HorizontalStickyHeaderLayout"

CocoaPods

pod "HorizontalStickyHeaderLayout"

开发

  • Xcode 12
  • Swift 5.3

许可证

MIT