swift - 如何使用 tochesBegan() 方法作为类的扩展?

标签 swift sprite-kit extension-methods

如何将 touchesBegan() 方法添加到我的类中?

这是我的方法:

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

    for touch in touches{

        let locationUser = touch.location(in: self)

        }}

最佳答案

您可以使用类扩展在其中添加方法,如果您 类是 UIView 或 UIWindow 的子类:

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")
    }
}

关于swift - 如何使用 tochesBegan() 方法作为类的扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981652/

相关文章:

ios - 如何在协议(protocol)中声明通用协议(protocol)属性要求

Swift - 叠加层未显示在 AVCaptureVideoPreviewLayer 上

swift SpriteKit : How to make a sprite gain slight velocity when it collides with an object?

swift - 让物体从屏幕边缘反弹?

c# - 实现扩展方法

vb.net - MVC 扩展方法被加载两次

Ajax.ActionLink 没有 href 属性以获得更好的 SEO

ios - 在 IOS Swift 中检测屏幕解锁事件

ios - viewDidLoad() 和 viewDidAppear() 只调用一次?

swift - 快速测试节点是否附加到物理主体