【发布时间】:2016-03-25 08:26:12
【问题描述】:
我有一个自定义按钮,它被添加到一个静态单元格中。不知何故,每当我点击按钮时,它不会像将按钮添加到普通视图时那样立即做出反应。
点击时,它会立即注册点击,但背景颜色在更改之前会稍有延迟。
要更改颜色,我在UIButton 的子类中有以下代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
border.fillColor = .redColor() // border is a CAShapeLayer
print("began") // This prints right when the button is clicked
}
这个按钮被添加到UITableViewCell 的子类中,并且按钮正在做出反应,除了改变颜色有延迟。我该如何解决这个问题?
答案(果冻):
Jelly 的回答对我帮助很大,让按钮的反应更好一些。了解问题所在也可以更轻松地研究问题并发现按钮的反应就像在普通视图中一样,我必须在表格视图及其子视图中禁用 delaysContentTouches:
tableView.delaysContentTouches = false
tableView.subviews.forEach { ($0 as? UIScrollView)?.delaysContentTouches = false }
【问题讨论】:
-
为什么不用
UIButton:addTarget方法而不是点击
标签: ios swift uitableview button