MSPeekCollectionViewDelegateImplementation 3.2.0

MSPeekCollectionViewDelegateImplementation 3.2.0

MaherKSantina 维护。



  • Maher Santina

MSPeekCollectionViewDelegateImplementation

Version 3.0.0 是新的!🎉

窥视逻辑现在是使用自定义的 UICollectionViewLayout 完成的,这使得集成更容易,并会引入更少的错误!(并且希望能解决您遇到的所有问题)

从 2.0.0 迁移到 3.0.0

我尽量减少从 V2 迁移到 V3 的工作量。以下是步骤:

1- 将 MSPeekCollectionViewDelegateImplementation 初始化替换为 MSCollectionViewPeekingBehavior

2- 在您的 collectionView 上,调用 configureForPeekingBehavior 如此:

collectionView.configureForPeekingBehavior(behavior: behavior)

3- 将集合视图的代理设置为您查看器(或您想要的其他任何类)

4- 在集合视图代理函数 scrollViewWillEndDragging 中,调用行为的 scrollViewWillEndDragging 如此:

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

5- ???

6- 完成💰

您可以通过查看示例来获取详细使用方法。

介绍

Build Status

ezgif-2-9f7a86182f

当前的设计趋势要求复杂的设计,可以实现垂直滚动容器中的水平滚动。因此,为了向用户展示他们可以垂直滚动,应该在侧面显示一个预览项。这个库就是这样做的。我编写这个库是因为没有现成的库能实现这个简单功能。此外,其他库要求我继承自 UICollectionViewController,这在我需要从其他 View Controller 继承时并没有太多的灵活性。

示例

要运行示例项目,首先克隆仓库,然后从 Example 目录中运行 pod install

要求

  • XCode 11.2.1
  • Swift 5

此 pod 可能能在旧版本的 XCode 上工作,但尚未进行测试。

安装

MSPeekCollectionViewDelegateImplementation 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的 Podfile 中

pod 'MSPeekCollectionViewDelegateImplementation'

用法

Storyboard

  1. 拖放一个 UICollectionView

  2. 将集合视图的单元格的重用标识符设置为 Cell

  3. 为集合视图创建一个引用

@IBOutlet weak var collectionView: UICollectionView!
  1. 将集合视图绑定到输出

  2. 导入库

import MSPeekCollectionViewDelegateImplementation
  1. 创建一个 MSCollectionViewPeekingBehavior 类型的变量
var behavior: MSCollectionViewPeekingBehavior!
  1. viewDidLoad() 中,初始化行为并配置 collectionView 的预览行为
behavior = MSCollectionViewPeekingBehavior()
collectionView.configureForPeekingBehavior(behavior: behavior)

或者您可以使用下面的参数中的任意组合(根据需要组合)

behavior = MSCollectionViewPeekingBehavior(cellSpacing: 10)
behavior = MSCollectionViewPeekingBehavior(cellPeekWidth: 20)
//minimumItemsToScroll is the minimum number of items that can be scrolled
behavior = MSCollectionViewPeekingBehavior(minimumItemsToScroll: 1)
//maximumItemsToScroll is the maximum number of items that can be scrolled if the scroll distance is large
behavior = MSCollectionViewPeekingBehavior(maximumItemsToScroll: 3)
//numberOfItemsToShow is the number of items that will be shown at the same time.
behavior = MSCollectionViewPeekingBehavior(numberOfItemsToShow: 3)

peek explanation

  1. viewDidLoad() 中,将集合视图的代理设置为 self
collectionView.delegate = self
  1. 在集合视图的代理函数 scrollViewWillEndDragging 中,像这样调用行为的 scrollViewWillEndDragging
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        behavior.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
}
  1. 将数据源实现作为 ViewController 的扩展创建
extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        //TODO: Configure cell
        return cell
    }
}
  1. viewDidLoad() 中,将集合视图的数据源设置为 self
collectionView.dataSource = self

工作示例

import UIKit
import MSPeekCollectionViewDelegateImplementation

class ViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var behavior = MSCollectionViewPeekingBehavior()

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.configureForPeekingBehavior(behavior: behavior)
        collectionView.delegate = self
        collectionView.dataSource = self
    }
}

extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        cell.contentView.backgroundColor = UIColor.red
        return cell
    }
}

extension ViewController: UICollectionViewDelegate {
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        behavior.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        print(behavior.currentIndex)
    }
}

特性

滚动到特定项

MSCollectionViewPeekingBehavior 现在有一个滚动到特定索引的函数

public func scrollToItem(at index: Int, animated: Bool)

您可以这样操作:

behavior.scrollToItem(at: 1, animated: true)

监听索引变化

您可以使用滚动视图的代理函数来完成此操作(确保您符合 UICollectionViewDelegate

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    print(behavior.currentIndex)
}

自定义

垂直滚动方向

实现支持带有垂直方向的集合视图,并将自动正确定位单元格,您可以使用以下方法设置垂直滚动和预览:

delegate = MSCollectionViewPeekingBehavior(scrollDirection: .vertical)
collectionView.configureForPeekingBehavior(behavior: behavior)

作者

Maher Santina,[邮箱地址保护]

赞助商

如果您喜欢这个仓库,我将非常感激您(橙汁)赞助我,以便我能够继续支持这个项目。我还在努力添加更多类似的可重用UI元素,使开发工作更加轻松。请参阅我的赞助页面了解更多详情。

贡献

任何贡献都将受到高度赞赏,请参阅CONTRIBUTING.md以获取更多信息。

许可

MSPeekCollectionViewDelegateImplementation以MIT许可提供。有关更多信息,请参阅LICENSE文件。