首页 > 解决方案 > 是否可以在另一个协议的扩展内实现一个协议的功能?

问题描述

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

我之前所做的是在我的 CustomRefreshControl 中创建实现 UIScrollViewDelegate 并仅覆盖 scrollViewMethods 并传递我的 customRefresh 控件中的所有值,但是这种方法有点难看而且不实用,因为我必须为每个应用它的类都这样做CustomRefreshControl 所以我尝试使用协议扩展来解决它,但它不起作用......

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 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 }
    // ...
}

任何人都知道如何使这项工作或为什么它不工作?

标签: swiftprotocols

解决方案


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


推荐阅读