ios - Swizzling UIResponder Touch 事件不调用原始方法实现

标签 ios swift uitouch objective-c-runtime method-swizzling

我正在尝试通过创建 UIResponder 扩展来调整 UITouch 生命周期事件。以下是描述 touchesBegan 实现的摘录。

 public extension UIResponder {

   private enum TouchPhase: String {
    case begin
    case move
    case end
    case cancel
    //computed property
    var phase: String {
        return self.rawValue
    }
}

    @objc func swizztouchesBegan(_ touches: Set<UITouch>, with event: 
            UIEvent?) {
         print("swizztouchesBegan event called!")
         let touch = touches.first! as UITouch
         self.createTouchesDict(for: touch, event: event, phase: .begin)
         self.swizztouchesBegan(touches, with: event)
     }

    static func swizzleTouchesBegan() {
       let _: () = {
            let originalSelector = 
        #selector(UIResponder.touchesBegan(_:with:))
            let swizzledSelector = 
        #selector(UIResponder.swizztouchesBegan(_:with:))
             let originalMethod = class_getInstanceMethod(UIResponder.self, originalSelector)
             let swizzledMethod = class_getInstanceMethod(UIResponder.self, swizzledSelector)
    method_exchangeImplementations(originalMethod!, swizzledMethod!)
    }()
 }
}

然后我像这样在我的应用程序中调用它-

 [UIResponder swizzleTouchesBegan]; //my app is a mix of Objective-C and Swift

我看到的是 - “swizztouchesBegan 事件已调用!”被打印出来,我开始创建触摸元数据的字典。但是,它看起来并没有调用原始实现,例如假设在我的代码中的某处,在 TableView Controller 中,我编写了以下内容-

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    print("AM I GETTING CALLED?")
} 

未调用此覆盖的 touchesBegan - 可能是什么问题?我已经通过再次调用调配的实现从调配的内部调用原始实现(从而确保调用原始实现。)

PS/附录:我已经能够通过覆盖 UIView 扩展中的触摸方法来让它工作,如下所示。有人可以指出使用这种方法与 swizzling 方法相比是否有任何缺点吗?

public extension UIView {

private enum TouchPhase: String {
    case begin
    case move
    case end
    case cancel
    //computed property
    var phase: String {
        return self.rawValue
    }
}
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    print("overriding touches began event!")
    let touch = touches.first! as UITouch
    self.createTouchesDict(for: touch, event: event, phase: .begin)
    super.touchesBegan(touches, with: event)
}

open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("overriding touches moved event!")
    let touch = touches.first! as UITouch
    self.createTouchesDict(for: touch, event: event, phase: .move)
    super.touchesMoved(touches, with: event)

}

open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("overriding touches ended event!")
    let touch = touches.first! as UITouch
    self.createTouchesDict(for: touch, event: event, phase: .end)
    super.touchesEnded(touches, with: event)

}

  open override func touchesCancelled(_ touches: Set<UITouch>, with 
 event: UIEvent?) {
    print("overriding touches canceled event!")
    let touch = touches.first! as UITouch
    self.createTouchesDict(for: touch, event: event, phase: .cancel)
    super.touchesCancelled(touches, with: event)

    }
  }

最佳答案

在我看来,您总是在调整选择器而不检查是否已经完成。

这里有一个关于如何做到这一点的示例。

extension UIResponder {

    static let swizzleIfNeeded: () = {
        swizzle(originalSelector: #selector(touchesBegan(_:with:)), to: #selector(swizztouchesBegan(_:with:)))
    }()

    static func swizzle(originalSelector: Selector, to swizzledSelector: Selector) {
        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
        let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
        } else {
            method_exchangeImplementations(originalMethod!, swizzledMethod!);
        }
    }

    @objc func swizztouchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("swizztouchesBegan event called!")
        let touch = touches.first! as UITouch
        self.createTouchesDict(for: touch, event: event, phase: .begin)
        self.swizztouchesBegan(touches, with: event)
    }
}

然后在你的 app delegate 中确实完成了(例如)

UIResponder.swizzleIfNeeded

关于ios - Swizzling UIResponder Touch 事件不调用原始方法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51254471/

相关文章:

iphone - 使用触摸事件的 locationInView 时,如何确定设备上 CGPoint 的有效范围?

ios - 将 Facebook SDK `activateApp` 添加到 `applicationDidBecomeActive` 吗?

ios - 如何获取 Google Maps API iOS 中某个点的坐标?

swift - Drupal 服务 API 使用 xcode 基本身份验证返回 "access denied for user anonymous"

ios - Swift 中的委托(delegate)错误

iphone - 通过触摸平滑旋转 UIView

ios - 为什么 CoreAnimation 似乎在缓存表示层副本?

ios - 有没有全局性的感动事件?

swift - swiftc -target 和 -target-cpu 选项可以使用哪些目标?

ios - 如何继续绘制Rect : when finger on screen