gpt4 book ai didi

ios - 如何单击位于 UITableView 下方的按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:11:57 24 4
gpt4 key购买 nike

比如说,我在 UITableView 下面有一个按钮,我怎样才能通过 UITableViewCell 单击按钮而不触发单元格单击事件:

enter image description here

我把按钮放在tableview后面的原因是我想看到并点击颜色设置为清晰的单元格下面的按钮,当我滚动表格时,按钮可以被没有的单元格覆盖清晰的颜色

最佳答案

我创建了一个示例项目并使其运行:

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

let tap = UITapGestureRecognizer(target: self, action: #selector(TableViewVC.handleTap))
tap.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tap)
}

func handleTap(touch: UITapGestureRecognizer) {
let touchPoint = touch.locationInView(self.view)
let isPointInFrame = CGRectContainsPoint(button.frame, touchPoint)

print(isPointInFrame)

if isPointInFrame == true {
print("button pressed")
}
}

要检查按钮是否真的被按下,我们需要使用长按手势:

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

let tap = UILongPressGestureRecognizer(target: self, action: #selector(TableViewVC.handleTap))
tap.minimumPressDuration = 0.01
self.view.addGestureRecognizer(tap)
}


func handleTap(touch: UILongPressGestureRecognizer) {
let touchPoint = touch.locationInView(self.view)

print(" pressed")
if touch.state == .Began {
let isPointInFrame = CGRectContainsPoint(button.frame, touchPoint)
print(isPointInFrame)

if isPointInFrame == true {
print("button pressed")
button.backgroundColor = UIColor.lightGrayColor()
}
}else if touch.state == .Ended {

button.backgroundColor = UIColor.whiteColor()
}
}

关于ios - 如何单击位于 UITableView 下方的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462090/

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