gpt4 book ai didi

ios - 手势识别器没有响应

转载 作者:行者123 更新时间:2023-11-28 10:58:04 24 4
gpt4 key购买 nike

我的 View Controller 上有一个按钮,它引入了一个菜单和一个稍微透明的 View blackView - 菜单类包括一个轻击手势识别器,用于在 blackView< 时关闭菜单 被窃听。但是它没有响应,我不确定我的问题出在哪里。这是菜单的代码:

class MenuController: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

let blackView = UIView()

let collectionView: UICollectionView = {

let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()

let cellId = "cellId"


func presentMenu() {

if let window = UIApplication.shared.keyWindow {

blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.isUserInteractionEnabled = true
// Tap outside to dismiss
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

window.addSubview(blackView)
window.addSubview(collectionView)

let height: CGFloat = 200
let y = window.frame.height - height
collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

blackView.frame = window.frame
blackView.alpha = 0

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1

self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

}, completion: nil)
}
}


func handleDismiss() {
print("working")

UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {

self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

}
}
}


// Collection view data sources
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}



override init() {
super.init()

collectionView.dataSource = self
collectionView.delegate = self

collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
}



}

presentMenu 被调用,当我点击我的 View Controller 上的必要按钮时,菜单出现。但是 handleDismiss 没有被调用。感谢您的帮助,希望这只是初学者的错误!

解决方案后编辑:我在我的 View Controller 中初始化 menuController(),如下所示:

func showMenu() {
let menuController = MenuController()

menuController.presentMenu()

}

并将其更改为@ronatory 的解决方案

lazy var menuController: MenuController = {
let menuController = MenuController()
return menuController
}()

func showMenu() {
menuController.presentMenu()
}

解决了。

最佳答案

所以我发现的唯一方法是更改​​ menuController 的初始化。而不是在 showMenu() 方法中初始化它(问题是你在方法中初始化它并且在方法执行后 menuController 为 nil):

func showMenu() {
let menuController = MenuController()
menuController.presentMenu()
}

将其替换为使用惰性存储属性(以便在方法执行后也可以访问实例)并且也无需设置 blackView.isUserInteractionEnabled = true。您可以删除它:

lazy var menuController: MenuController = {
let menuController = MenuController()
return menuController
}()

func showMenu() {
menuController.presentMenu()
}

就是这样。进行此更改后,您的点击手势识别器应该可以工作了。我用你的项目测试了它。如果您将项目的这一部分作为代码添加到问题中,那就太好了,这样其他人就可以更好地理解问题而无需下载项目

关于ios - 手势识别器没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42077539/

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