尝试单击以编程方式创建的视图时出错 swift 3

Error when trying to click in a view created programmatically swift 3

我以编程方式创建了一个视图,就像顶部的弹出窗口一样,其中有一些文本和图像!

alert = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width , height: 0))
alert?.frame.origin.y = (textLable?.frame.height)!
alert?.frame.size.height = (textLable?.frame.height)!
alert?.backgroundColor = self.arrayOptions.colorBackground

然后我尝试在 init 中调用的设置函数中像这样向该视图添加 UITapGestureRecognizer。

let tap = UITapGestureRecognizer(target: self, action: #selector(self.teste))
tap.numberOfTapsRequired = 1
alert?.addGestureRecognizer(tap)

我在 UitableViewController 中像这样添加该视图:

self.popView = PopupView(frame: CGRect(x: 0 , y: 0 , width:self.view.frame.width, height: 0), with: PopUpOptions.error, originY: 0,description:"blablabla")
self.view.addSubview(self.popView!)

但是当我点击视图时没有任何反应,但是当我反复点击时出现这个错误:

<_UISystemGestureGateGestureRecognizer: 0x174186f50>: Gesture: Failed to receive system gesture state notification before next touch

但我似乎找不到这个问题的答案,请问谁能帮帮我! 谢谢!

这是我的框架 GitHub link https://github.com/Coimbraa/AlertsPopup_framework

试试这个:

alert.isUserInteractionEnabled = true

将此 属性 设置为 true 以获得您想要点击的那些视图(在其中添加点击手势识别器)。

您正在 Table 视图控制器中添加自定义警报视图。

先试试Tung Fam的建议,如果不行,

尝试在 window 上添加它,例如:

self.view.window.addSubview(self.popView!)

同时将 window 的 frame 设置为 popupView。

据我所知,您在

中将 TapGesture 的目标设置为自己

let tap = UITapGestureRecognizer(target: self, action: #selector(self.teste))

这里 self 是 UIView 的对象(您的警报视图)。

所以根据这一行,您的 tapGesture 侦听器必须在 alert? class 中实现。并且您需要使用自定义协议或自定义委托来获取其他 classes.

中的 tapGesture 的操作

查看了您的 GitHub 存储库 - 一些注释...

我在您的代码中看到很多“!”。其中每一个都是潜在的崩溃。例如,我必须进行一些编辑才能使其完全达到 运行(我没有代码期望的图像资源)。

PopUpView.swift 中将 init() 函数更改为此(刚刚添加了 clipsToBounds 行):

override public init(frame: CGRect) {
    super.init(frame: frame)
    setup()
    self.clipsToBounds = true
}

现在,当您调用 popView?.toggleStatus() 时,您可能不会看到 任何东西

您的 PopupView "container" 的高度为零。如果不剪辑其内容,您可以看到 内容,但无法与之交互。

我将 toggleStatus() 中的动画块更改为:

    UIView.animate(withDuration: 0.5, animations: {
        if let sz = self.alert?.frame.size {
            self.frame.size.height = sz.height
        }
        self.alert?.frame.origin.y =  (self.startY)! + (self.status ? 0 : -((self.textLable?.frame.height)! + self.startY))
    }) { (finished:Bool) in
    // ...

我现在可以点击并编辑 TextView,然后点击其他地方并将 print("pressed") 输出到调试控制台。

我没有深入挖掘,所以我不知道 if/where 你需要添加额外的代码来将帧高度重置为零(或者它可能被隐藏或删除,无论如何)。