ios – Methodology Swizzling Not Calling Authentic touchesBegan and touchesEnded Strategies in Swift

0
2
ios – Methodology Swizzling Not Calling Authentic touchesBegan and touchesEnded Strategies in Swift


I’m attempting to implement methodology swizzling in Swift to intercept touchesBegan and touchesEnded strategies throughout all UIResponder situations. Nonetheless, I am encountering points the place the unique strategies contained in the UIViewContoller usually are not being known as,

In brief

The swizzled strategies usually are not calling the unique touchesBegan and touchesEnded strategies in my UIViewController

Right here is the code I am utilizing:

extension UIResponder {
    static let swizzleTouchMethods: Void = {
        let originalBeganSelector = #selector(touchesBegan(_:with:))
        let swizzledBeganSelector = #selector(xxx_touchesBegan(_:with:))

        guard let originalBeganMethod = class_getInstanceMethod(UIView.self, originalBeganSelector),
              let swizzledBeganMethod = class_getInstanceMethod(UIView.self, swizzledBeganSelector) else {
            return
        }

        method_exchangeImplementations(originalBeganMethod, swizzledBeganMethod)

        let originalMovedSelector = #selector(touchesMoved(_:with:))
        let swizzledMovedSelector = #selector(xxx_touchesMoved(_:with:))

        guard let originalMovedMethod = class_getInstanceMethod(UIView.self, originalMovedSelector),
              let swizzledMovedMethod = class_getInstanceMethod(UIView.self, swizzledMovedSelector) else {
            return
        }

        method_exchangeImplementations(originalMovedMethod, swizzledMovedMethod)

        let originalEndedSelector = #selector(touchesEnded(_:with:))
        let swizzledEndedSelector = #selector(xxx_touchesEnded(_:with:))

        guard let originalEndedMethod = class_getInstanceMethod(UIView.self, originalEndedSelector),
              let swizzledEndedMethod = class_getInstanceMethod(UIView.self, swizzledEndedSelector) else {
            return
        }

        method_exchangeImplementations(originalEndedMethod, swizzledEndedMethod)
    }()

    @objc func xxx_touchesBegan(_ touches: Set, with occasion: UIEvent?) {
        if let contact = touches.first {
            print("Swizzled contact started at: radius (contact.majorRadius)")
        }

        // Name the unique methodology (which is now swizzled)
        self.xxx_touchesBegan(touches, with: occasion)
    }

    @objc func xxx_touchesMoved(_ touches: Set, with occasion: UIEvent?) {
        if let contact = touches.first {
            print("Swizzled contact moved at: radius (contact.majorRadius)")
        }

        // Name the unique methodology (which is now swizzled)
        self.xxx_touchesMoved(touches, with: occasion)
    }

    @objc func xxx_touchesEnded(_ touches: Set, with occasion: UIEvent?) {
        if let contact = touches.first {
            print("Swizzled contact ended at: radius (contact.majorRadius)")
        }

        // Name the unique methodology (which is now swizzled)
        self.xxx_touchesEnded(touches, with: occasion)
    }
}

ultimate class MyViewController: UIViewController { 

  override func viewDidLoad() {
     tremendous.viewDidLoad()
     UIResponder.swizzleTouchMethods
  }

 override func touchesBegan(_ touches: Set, with occasion: UIEvent?) {
    tremendous.touchesBegan(touches, with: occasion)
    print("unique touchesBegan occasion")
   
}

 override func touchesEnded(_ touches: Set, with occasion:  UIEvent?) {
    tremendous.touchesEnded(touches, with: occasion)
    print("unique touchesEnded occasion")
  
  }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here