如何使用 tochesBegan() 方法作为 class 的扩展?

How to use a tochesBegan() method as an extension of a class?

如何向我的 class 添加 touchesBegan() 方法?

这是我的方法:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches{

        let locationUser = touch.location(in: self)

        }}

您可以使用 class 扩展,如果您的 class 是 UIView 或 UIWindow 的子class:

extension YourClass {
    override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        for touch in touches{
            let locationUser = touch.location(in: self)
        }
        print("touchesBegan")
    }
}