gpt4 book ai didi

ios - 在 topViewController 中添加时,UIView 手势识别器不起作用

转载 作者:行者123 更新时间:2023-11-28 05:51:39 27 4
gpt4 key购买 nike

我正在尝试在最顶层的 ViewController 上添加一个 View 。它确实有效,但是当我尝试添加点击手势识别器时,该操作不起作用。这是我的代码,我使用共享实例调用此类。难道是因为对象没有被保留?

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)
}

}
}

最佳答案

我刚刚发现了问题。当 View 处于动画状态时,您无法与其进行交互,事实就是如此。所以我只是创建了一个容器 View 来保存通知卡并将手势识别器放在容器上而不是正在动画的 View 上。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com