获取触摸UIView的手指数

Mar*_*dor 2 iphone uiview uitouch ios swift

我想根据当前触摸的次数更改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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我正在努力实现多点触控。我觉得我不明白这里的内容。

UIView是否有一个属性可以检查当前有多少手指在触摸它?

我可以检查是否每次发生新的touchesBegan或touchesEnded。

abd*_*lek 5

首先在子类上启用多点触控

self.isMultipleTouchEnabled = true
Run Code Online (Sandbox Code Playgroud)

然后,在您的触摸事件函数中,您可以从UIView类中获取事件的所有触摸。

let touchCount = event?.touches(for: self)?.count
Run Code Online (Sandbox Code Playgroud)