I am trying to swizzle the UITouch
lifecycle events by creating a UIResponder
extension. Following is an excerpt depicting the implementation for 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!)
}()
}
}
I am then invoking it in my app like so-
[UIResponder swizzleTouchesBegan]; //my app is a mix of Objective-C and Swift
What I see is- "swizztouchesBegan event called!" gets printed and I get to create the dictionary of the touch metadata. However, it looks like it does not call the original implementation, e.g. let's say somewhere in my code, inside a table view controller, I write the following-
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
print("AM I GETTING CALLED?")
}
this overridden touchesBegan is not getting called- what could be the issue? I am already calling the original implementation from inside the swizzled one by calling the swizzled one again (thereby making sure the original one gets called.)
PS/ Addendum: I have been able to get it working by just overriding the touch methods in an UIView
extension, like following. Can someone point out if there's any disadvantage using this approach over the swizzling one?
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)
}
}