Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIView手势识别器在topViewController中添加时不工作_Ios_Swift_Swift4_Uigesturerecognizer - Fatal编程技术网

Ios UIView手势识别器在topViewController中添加时不工作

Ios UIView手势识别器在topViewController中添加时不工作,ios,swift,swift4,uigesturerecognizer,Ios,Swift,Swift4,Uigesturerecognizer,我正在尝试在最顶部的ViewController上添加视图。它实际上是有效的,但当我试图添加一个轻拍手势识别器时,这个动作不起作用。这是我的代码,我使用共享实例调用这个类。可能是因为该对象未被保留 class QFNotificationView: NSObject { static let shared = QFNotificationView() internal let keyWindow = UIApplication.shared.keyWindow! private var t

我正在尝试在最顶部的ViewController上添加视图。它实际上是有效的,但当我试图添加一个轻拍手势识别器时,这个动作不起作用。这是我的代码,我使用共享实例调用这个类。可能是因为该对象未被保留

class QFNotificationView: NSObject {

static let shared = QFNotificationView()

internal let keyWindow = UIApplication.shared.keyWindow!

private var titleLabel: UILabel = {
    let windowFrame = UIApplication.shared.keyWindow!.bounds
    let label = UILabel(frame: CGRect(x: 12, y: 15, width: windowFrame.width - 24, height: 20))
    label.font = UIFont(name: "WorkSans-SemiBold", size: 18)
    label.textColor = .darkishPurple
    return label
}()

private var descriptionLabel: UILabel = {
    let windowFrame = UIApplication.shared.keyWindow!.bounds
    let label = UILabel(frame: CGRect(x: 12, y: 35, width: windowFrame.width - 24, height: 40))
    label.font = UIFont(name: "WorkSans-Regular", size: 14)
    label.numberOfLines = 2
    label.textColor = .darkLavender
    return label
}()

private var notificationView: UIView = {
    let windowFrame = UIApplication.shared.keyWindow!.bounds
    let view = UIView(frame: CGRect(x: 0, y: -82, width: windowFrame.width, height: 82))
    view.backgroundColor = .white
    view.isUserInteractionEnabled = true
    return view
}()

var message: Message?

func show(message: Message) {
    self.message = message
    titleLabel.text = "Message"
    descriptionLabel.text = message.messageText
    notificationView.addSubview(titleLabel)
    notificationView.addSubview(descriptionLabel)
    if let topVC = UIApplication.getTopMostViewController() {
        topVC.view.addSubview(notificationView)
        notificationView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTap)))
        animateNotification()
    }

}

private func animateNotification() {
    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
        self.notificationView.frame = CGRect(x: 0, y: 0, width: self.keyWindow.bounds.width, height: 82)
    }) { (_) in
        UIView.animate(withDuration: 0.5, delay: 4, options: .curveEaseInOut, animations: {
            self.notificationView.frame = CGRect(x: 0, y: -82, width: self.keyWindow.bounds.width, height: 82)
        }) { (_) in
            self.notificationView.removeFromSuperview()
        }
    }
}

@objc func didTap() {
    print("CLICKEDD")
    if let topVC = UIApplication.getTopMostViewController() {
       let dest = ChatViewController.instantiateFromAppStoryboard(appStoryboard: .Main)
        dest.serviceRequestID = message?.serviceRequest
        topVC.navigationController?.pushViewController(dest, animated: true)
    }

}
}

我刚发现问题所在。当视图被设置动画时,您不能与视图交互,事实就是这样。因此,我只是创建了一个容器视图来保存通知卡,并将手势识别器放置在容器上,而不是正在设置动画的视图