[英]Add a custom view to the UIViewController from the custom view class
I have a custom view.我有一个自定义视图。 I named AnimatingView
.我命名为AnimatingView
。 I have customized the view in the AnimatingView.swift
.我在AnimatingView.swift
中自定义了视图。 In the ViewController, I am initiating the view with在 ViewController 中,我正在启动视图
let customAnimatingView = AnimatingView(overView: self.view)
and then I add the subView with -然后我添加子视图 -
self.view.addSubview(customAnimatingView)
However, I don't want the user add it as a subView like above, rather.但是,我不希望用户像上面那样将它添加为子视图。 I want to call a func from AnimatingView
and the custom view should add it the controller like -我想从AnimatingView
调用一个函数,自定义视图应该将它添加到 controller 中,比如 -
customAnimatingView.preset()
Can anyone give me some hint on how can I achieve such behavior.谁能给我一些关于如何实现这种行为的提示。
Inside the custom view do在自定义视图中做
// 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)
}
OR if you need to send the main view instead或者如果您需要发送主视图
var parentView:UIView?
func present() {
parentView?.addSubview(self)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.