gpt4 book ai didi

ios - 将 GestureRecognizer 添加到 UIView 不会触发操作

转载 作者:可可西里 更新时间:2023-11-01 01:56:56 52 4
gpt4 key购买 nike

我不知道为什么这个手势识别器没有按预期工作:

class SlideInMenuLauncher: NSObject, UITableViewDelegate, UITableViewDataSource {

fileprivate let dimmerView = UIView()
fileprivate let tableView: UITableView = {
let tbv = UITableView(frame: .zero)
return tbv
}()
fileprivate let resourceArray: [Content]
weak var delegate: SlideInMenuDelegate?

let cellIdentifier = "SlideInMenuTableViewCell"

init(withContentArray contentArray: [Content]) {

resourceArray = contentArray

super.init()

tableView.dataSource = self
tableView.delegate = self

tableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)
}

func showMenu() {
if let window = UIApplication.shared.keyWindow {
dimmerView.backgroundColor = UIColor(white: 0, alpha: 0.5)

dimmerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissMenu)))
dimmerView.isUserInteractionEnabled = true

window.addSubview(dimmerView)
window.addSubview(tableView)

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

dimmerView.frame = window.frame
dimmerView.alpha = 0

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.dimmerView.alpha = 1
self.tableView.frame = CGRect(x: 0, y: y, width: self.tableView.frame.width, height: self.tableView.frame.height)
}, completion: nil)

}
}

func dismissMenu() {
UIView.animate(withDuration: 0.5) {
self.dimmerView.alpha = 0

if let window = UIApplication.shared.keyWindow {
self.tableView.frame = CGRect(x: 0, y: window.frame.height, width: self.tableView.frame.width, height: self.tableView.frame.height)
}
}
}

我在 dismissMenu 函数中放置了一个断点,但它从未被触发。也许我疲惫的眼睛(和头脑)错过了一些简单的东西?

在这里我添加了我实际调用这个类的代码,以防出现问题:

///Some other VC
let slideInMenuLauncher = SlideInMenuLauncher(withContentArray: [content])
slideInMenuLauncher.showMenu()

最佳答案

我希望这会产生编译器错误。您在 dismissMenu() 函数中缺少 @objc 声明。

我在 Storyboard 中向 View Controller 添加了一个 View ,并使用了您的代码的一个小变体,它可以很好地响应点击。因此,我的猜测是您将 View 添加到 View Controller 的方式有问题。

编辑:

我知道问题出在哪里:如果将 View 的 alpha 设置为 0,它会停止响应点击。尝试将 View 的不透明标志设置为 false,并将其背景颜色设置为 clearColor。

这是我创建的测试项目的代码:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var tapView: UIView!

override func viewDidLoad() {
super.viewDidLoad()

tapView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
tapView.isUserInteractionEnabled = true
}

@objc func handleTap() {
print("You tapped on the view")
}
}

关于ios - 将 GestureRecognizer 添加到 UIView 不会触发操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51887006/

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