如何使现有 class 成员成为协议实现?

How can I make an existing class member be a protocol implementation?

我的问题很简单。我有一个 class 管理我所有的 viewcontroller 到 viewcontroller 过渡动画,它需要 class 协议,因为我想让它便携,我通常制作带有项目特定内容的基础viewcontroller。

协议是这样的:

protocol AnimatedViewController {
    var view: UIView { get set }
    func animateViews()
}

但是当我让 UIViewController 继承自此时,我得到 view 未定义的错误。当我定义它时,它告诉我它已经定义了。

我怎样才能在我的协议中定义 view 并让它成为 UIViewController 中已经定义的 view

PS: 不太会起名字,欢迎指正

view in UIViewController 是展开的力 UIView,所以只需将协议定义为:

protocol AnimatedViewController {
    var view: UIView! { get set }
    func animateViews()
}

UIViewController中的view将用于满足此协议的要求。

例如:

class MyController: UIViewController, AnimatedViewController {

    func animateViews() {
        // do your stuff
    }
}

您可以在协议中使用 where 子句,不需要 view 属性。

protocol AnimatedViewController where Self:UIViewController {
    func animateViews()
}

class TVC : UIViewController, AnimatedViewController {
    func animateViews() {

    }
}