gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:45:10 27 4
gpt4 key购买 nike

我正在尝试通过创建 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com