swift - 是否可以在另一种协议(protocol)的扩展内实现一种协议(protocol)的功能?

标签 swift protocols

我正在创建一个自定义刷新控件,我需要从 UITableViewController 获取一些 UIScrollViewDelegate 方法,

我之前所做的是将 UIScrollViewDelegate 实现到我的 CustomRefreshControl 中,然后重写 scrollViewMethods 并传递我的 customRefresh 控件中的所有值,但这种方法有点丑陋且不实用,因为我必须为每个类都执行此操作应用此 CustomRefreshControl 所以我尝试使用协议(protocol)扩展来解决它,但它不起作用......

class CustomRefreshControl: UIView {
    // initializer and all other properties here
    // ...
}

extension CustomRefreshControl: UIScrollViewDelegate {
    // Here my implementation inside custom refresh control so I can make it behave like I intend,
    // But since all scroll methods from UITableViewController are called in the controller itself and I could not figure out a way to send these events directly into my customRefresh
    // I implemented these methods here and just passing them from controller to my customRefresh

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        // doing some calculations here
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        // doing some calculations here
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // doing some calculations here
    }
}

我之前在 viewController 方面是如何做的...

class RefreshingTableViewController: UITableViewController {
    lazy var configuration: CustomRefreshControl.Configurations = {
        return CustomRefreshControl.Configurations(threshold: 160, scrollView: self, refreshAction: refreshData)
    }()

    lazy var refresh = CustomRefreshControl(configurations: configuration)

    // All other properties and delegate and datasource implementations
    //...
}

extension RefreshingTableViewController {
    // That was my previous implementation But this solution did please so much
    // Because I have to implement this in every single viewController that wants to use this CustomRefreshControl

    override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        refresh.scrollViewWillBeginDragging(scrollView)
    }

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        refresh.scrollViewDidScroll(scrollView)
    }

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

所以我现在正在尝试实现的解决方案是一个带有协议(protocol)的解决方案,但我不知道它是否可能,如下......

protocol CustomRefreshManagerProtocol where Self: UIScrollViewDelegate {
    var customRefresh: CustomRefreshControl { get }
}

extension CustomRefreshManagerProtocol  {
    // Here I'm trying to implement the UIScrollViewDelegate methods, since I specifing that Self: UIScrollViewDelegate I thought it might work
    // But none of these functions are being called, so that's what I'm trying to get to work without success.

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        customRefresh.scrollViewWillBeginDragging(scrollView)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        customRefresh.scrollViewDidScroll(scrollView)
    }

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

class RefreshingTableViewController: UITableViewController, CustomRefreshManagerProtocol {
    lazy var refresh = CustomRefreshControl(configurations: configuration)
    var customRefresh: CustomRefreshControl { refresh }
    // ...
}

任何人都知道如何使其工作或为什么它不起作用?

最佳答案

扩展根本无法覆盖方法(有时可以工作,但它不是定义的行为)。所以答案是否定的。

关于swift - 是否可以在另一种协议(protocol)的扩展内实现一种协议(protocol)的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59235407/

相关文章:

swift - AlamofireImage Collection 单元在下载后将图像放入 AutoPurgingImageCache

swift - 十进制小时显示 4 分 60 秒而不是 5 分钟

swift - 移至回收站时的 URL 书签数据

Swift tableView 行动态高度不起作用

swift - 如何解决这个双击问题?

transactions - 获取已发布交易的 MAPP 协议(protocol)是什么?

swift - 返回确认协议(protocol)的结构类型

protocols - 如何在 Swift 中将协议(protocol)作为参数传递

generics - 在 Swift 泛型类型的 where 子句中访问自定义协议(protocol)的关联类型

windows - 编写一个小的 Windows 脚本来与大型机和/或 Endevor 对话