我希望根据当前触控的次数改变UIView的颜色。我已经成功地创建了单点触控的情况:
class colorChangerViewClass: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .blue)
print("touchesBegan")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .green)
print("touchesEnded")
}
func setColor(color: UIColor) {
self.backgroundColor = color
}
}
但是我在实现多点触控时遇到了困难。我感觉自己对此不够理解。
UIView是否有属性可以检查当前有几个手指在触摸它?
我可以在每次发生新的touchesBegan或touchesEnded时进行检查。
event?.touches(for: self)?.count
可能不总是有效,event?.allTouches?.count
在touchesBegan
方法中也可能有帮助。 - kevinzhow