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

ios – 将Swift项目转换为易于使用的Cocoapods框架,第1张

概述所以我需要一些帮助,因为我无法弄清楚它并且花了很多时间在它上面已经没有任何结果.我在互联网上发现的大部分内容都是关于如何将一个快速项目转换为一个框架,但我的问题不是项目本身或 cocoapods,而是关于我应该如何以及我应该让开发人员访问的内容. 所以我构建了一个可以找到here的Swift框架,它是使用带有自定义flowlayout和侧面滑动功能的UICollectionView构建的. 问题是 所以我需要一些帮助,因为我无法弄清楚它并且花了很多时间在它上面已经没有任何结果.我在互联网上发现的大部分内容都是关于如何将一个快速项目转换为一个框架,但我的问题不是项目本身或 cocoapods,而是关于我应该如何以及我应该让开发人员访问的内容.

所以我构建了一个可以找到here的Swift框架,它是使用带有自定义flowlayout和侧面滑动功能的UICollectionVIEw构建的.

问题是我不知道如何让这对开发人员易于使用,我想使它的行为类似于UICollectionVIEw,您需要实现自定义委托和数据源,而不必使用默认的UICollectionVIEwDelegate和UICollectionVIEwDatasource,所以我可以过滤掉会导致意外行为或只是简单的事情.

那么这里有什么好方法呢?我正在考虑可能继承UICollectionVIEw,因此人们不必使用UICollectionVIEw(因为这可能会让人感到困惑),而是使用VerticalCardSwiperVIEw,它具有相同的行为,但有点受限.我只是不知道这是否是正确的方法.

任何提示,见解或好的例子都将深表感谢!

编辑1:我更新了项目,因此它已经具有框架结构,我只需要弄清楚我将如何向开发人员提供对自定义部件的访问权限.

编辑2:我得到了一个答案,但我认为这个问题可能很容易被误解.目标是使其行为和行为像UICollectionVIEw(与委托,数据源,…)但更有限,我想知道如何实现这一点,如果它甚至可能.

编辑3:好吧,我有大部分工作,我会在这里留下一个链接到Github在线,所以需要类似东西的人可以自己检查一下.基本上,我所做的是像这样定制VerticalCardSwiperDelegate和VerticalCardSwiperDatasource:

/// 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的子类,它执行相同的 *** 作,只是具有不同的名称,对开发人员来说更容易理解)委托/数据源函数.希望这有助于其他人.另外,如果您有更好的方法,请告诉我.

解决方法 看起来不错!

我想说如果你想公开UICollectionVIEw,那么就按照UItableVIEw和UIScrollVIEw子类委托的方法进行 *** 作.

protocol VerticalCardSwiperDelegate: UICollectionVIEwDelegate {  // ...

您还可以分别在VerticalCardSwiperDelegate和VerticalCardSwiperDataSource委托中继承UICollectionVIEwDelegate和UICollectionVIEwDataSource,以获得相同的效果. (如果你认为这样做是有道理的.)

protocol VerticalCardSwiperDelegate: UICollectionVIEwDelegate {  // ...

也就是说,您的组件非常具体,可能会暴露底层的UICollectionVIEw可能会导致问题.在这里,我将锁定所有实现细节,并使其成为VerticalCardSwiper.这为您提供了完成基础架构更改的灵活性.

例如,在您的委托中,您将传回VerticalCardSwiper而不是底层组件.

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

通常,您希望保持暴露的接口尽可能小.这是为了帮助指导开发人员(需要学习的东西较少),而对于维护和现场问题则更少.

对公共接口的一些评论会有所帮助.我很高兴我可以命令 – alt-click获得一些提示.你已经这样做了.

总结

以上是内存溢出为你收集整理的ios – 将Swift项目转换为易于使用的Cocoapods框架全部内容,希望文章能够帮你解决ios – 将Swift项目转换为易于使用的Cocoapods框架所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1100063.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存