Я пытаюсь реализовать метод, прописываемый в перехват Touchesbegan и Toucheseded, во всех экземплярах Uresponder. Тем не менее, я сталкиваюсь с проблемами, когда исходные методы внутри UiviewContoller не называются,
Короче говоря,
Свиджированные методы не вызывают оригинал touchesbegan и toucheseded в моем uiviewcontroler uiviewcontroller press
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 event: UIEvent?) {
if let touch = touches.first {
print("Swizzled touch began at: radius \(touch.majorRadius)")
}
// Call the original method (which is now swizzled)
self.xxx_touchesBegan(touches, with: event)
}
@objc func xxx_touchesMoved(_ touches: Set, with event: UIEvent?) {
if let touch = touches.first {
print("Swizzled touch moved at: radius \(touch.majorRadius)")
}
// Call the original method (which is now swizzled)
self.xxx_touchesMoved(touches, with: event)
}
@objc func xxx_touchesEnded(_ touches: Set, with event: UIEvent?) {
if let touch = touches.first {
print("Swizzled touch ended at: radius \(touch.majorRadius)")
}
// Call the original method (which is now swizzled)
self.xxx_touchesEnded(touches, with: event)
}
}
final class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIResponder.swizzleTouchMethods
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
print("original touchesBegan event")
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
print("original touchesEnded event")
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... thods-in-s
Метод Swizzling не вызывает оригинальных Touchesbegan и Touchesdended Methods в Swift ⇐ IOS
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение