从自定义视图向 UIViewController 添加自定义视图 class

Add a custom view to the UIViewController from the custom view class

我有自定义视图。我命名为AnimatingView。我在 AnimatingView.swift 中自定义了视图。在 ViewController 中,我正在使用

启动视图
let customAnimatingView = AnimatingView(overView: self.view)

然后我用 -

添加子视图
self.view.addSubview(customAnimatingView)

但是,我不希望用户像上面那样将其添加为子视图。我想从 AnimatingView 调用一个函数,自定义视图应该将它添加到控制器,如 -

customAnimatingView.preset()

任何人都可以给我一些关于如何实现这种行为的提示。

在自定义视图中做

// add a property that refers to the the vc container of the view
// init it when you create an instance of the view
weak var parentController:UIViewController?
func present() { 
  parentController?.view.addSubview(self)
}

或者如果您需要发送主视图

var parentView:UIView?
func present() { 
  parentView?.addSubview(self)
}