是否可以从另一个协议的扩展内部的一个协议实现功能?

时间:2019-12-08 12:31:42

标签: swift protocols

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

我之前所做的是将UIScrollViewDelegate实现实现到CustomRefreshControl中,并覆盖了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 }
    // ...
}

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

1 个答案:

答案 0 :(得分:0)

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