gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:30:59 25 4
gpt4 key购买 nike

我正在创建一个自定义刷新控件,我需要从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com