我试图在最顶部的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)
    }

}
}

最佳答案

我刚刚发现了问题。动画时,您无法与视图进行交互,事实就是如此。因此,我只是创建了一个容器视图来容纳通知卡,然后将手势识别器放置在容器上而不是正在动画的视图上。

关于ios - 在topViewController中添加时,UIView手势识别器不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52749347/

10-09 09:14