将Swift项目转换为易于使用的Cocoapods框架

7
所以我需要一些帮助,因为我无法真正弄清楚它,已经花了很多时间在上面,但没有任何结果。我在互联网上找到的大部分内容都是关于如何将Swift项目转换为框架的,但我的问题不在于项目本身或cocoapods,而是关于应该使开发人员可以访问什么以及如何访问。
所以我构建了一个Swift框架,您可以在这里找到它,它使用自定义 flowlayout 和侧滑功能构建了 UICollectionView
问题在于,我不知道如何使其易于开发人员使用,我想使其像 UICollectionView 一样运行,其中您需要实现自定义委托和数据源,而无需使用默认的 UICollectionViewDelegate UICollectionViewDatasource ,以便过滤掉可能导致意外行为的事物或保持简单。
那么在这里应该采取什么样的好方法呢?我考虑可能会对UICollectionView进行子类化,这样人们就不必使用UICollectionView(因为这可能会让人感到困惑),而是使用一个名为VerticalCardSwiperView的视图,它具有相同的行为,但功能有所限制。我只是不知道这是否是正确的方法。任何提示、见解或好的例子将不胜感激!

编辑 1:我已更新项目,使其具有框架结构,现在我只需要想办法让开发人员能够访问自定义部分。

编辑 2:我已经得到了一个答案,但我认为问题可能很容易被误解。目标是使其行为和表现类似于UICollectionView(具有委托、数据源等),但更加有限制,我想知道如何实现它,如果可能的话。

编辑 3:好的,我已经大部分完成了,我将在这里留下Github在线链接,以便需要类似功能的人可以自行查看。基本上,我已经创建了自定义的VerticalCardSwiperDelegateVerticalCardSwiperDatasource,就像这样:

/// This datasource is used for providing data to the `VerticalCardSwiper`.
public protocol VerticalCardSwiperDatasource: class {

    /**
     Sets the number of cards for the `UICollectionView` inside the VerticalCardSwiperController.
     - parameter verticalCardSwiperView: The `VerticalCardSwiperView` where we set the amount of cards.
     - returns: an `Int` with the amount of cards we want to show.
     */
    func numberOfCards(verticalCardSwiperView: VerticalCardSwiperView) -> Int

    /**
     Asks your data source object for the cell that corresponds to the specified item in the `VerticalCardSwiper`.
     Your implementation of this method is responsible for creating, configuring, and returning the appropriate `CardCell` for the given item.
     - parameter verticalCardSwiperView: The `VerticalCardSwiperView` that will display the `CardCell`.
     - parameter index: The that the `CardCell` should be shown at.
     - returns: A CardCell object. The default value is an empty CardCell object.
    */
    func cardForItemAt(verticalCardSwiperView: VerticalCardSwiperView, cardForItemAt index: Int) -> CardCell
}

然后我将它们钩在VerticalCardSwiper类中:

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return datasource?.numberOfCards(verticalCardSwiperView: verticalCardSwiperView) ?? 0
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    return (datasource?.cardForItemAt(verticalCardSwiperView: verticalCardSwiperView, cardForItemAt: indexPath.row))!
}

最后,我刚刚将UICollectionView子类化为VerticalCardSwiperView并将其作为参数传递(因为它基本上是UICollectionView的子类,所以它做相同的事情,只是有一个不同的名称,这对于开发人员来说更容易理解),用于委托/数据源函数。希望这能帮助其他人。另外,如果您有更好的方法,请告诉我。

1个回答

3

看起来很棒!

如果你想暴露UICollectionView,那么请遵循UITableView的做法,并使用UIScrollView子类化委托。

protocol VerticalCardSwiperDelegate: UICollectionViewDelegate {
  // ...

您还可以在 VerticalCardSwiperDelegateVerticalCardSwiperDataSource 代理中分别对UICollectionViewDelegateUICollectionViewDataSource进行子类化,以达到相同的效果(如果您认为这样做有意义)。

protocol VerticalCardSwiperDelegate: UICollectionViewDelegate {
  // ...

话虽如此,你的组件非常特定,可能公开底层的UICollectionView会导致问题。在这里,我会将所有实现细节锁定,并完全使用VerticalCardSwiper。这为你提供了灵活性,可以完全更改底层架构。

例如,在你的代理中,你会传回VerticalCardSwiper而不是底层组件。

func cardForItemAt(verticalCardSwiper: VerticalCardSwiper, cardForItemAt index: Int) -> CardCell {
  // ...

通常情况下,您希望尽可能地减小公开接口的规模。这有助于指导开发人员(需要学习的内容更少),同时减少您需要维护和回答问题的工作量。

对公共接口的一些注释会很有帮助。当我可以使用command-alt-click获取一些提示时,我会感到高兴。 您已经做到了这一点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接