1

我正在尝试UITouch通过创建UIResponder扩展来调整生命周期事件。以下是描述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 事件!” 被打印出来,我开始创建触摸元数据的字典。但是,它看起来并没有调用原始实现,例如,假设在我的代码中的某个地方,在表格视图控制器中,我编写了以下内容 -

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

这个被覆盖的 touchesBegan 没有被调用 - 可能是什么问题?我已经通过再次调用 swizzled 来从 swizzled 内部调用原始实现(从而确保调用原始实现。)

PS/附录:我已经能够通过覆盖UIView扩展中的触摸方法来使其工作,如下所示。有人可以指出使用这种方法比使用这种方法有什么缺点吗?

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)

    }
  }
4

2 回答 2

0

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

这是有关如何执行此操作的示例。

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)
    }
}

然后在您的应用程序委托中完成(例如)

UIResponder.swizzleIfNeeded
于 2018-07-09T22:19:50.263 回答
0

swift 似乎不需要方法 swizzle,因为有扩展。

因此,如果您想为任何课程调配触摸,您只需覆盖触摸即可。像下面我想给所有反弹效果

extension UIView {
    func highlight(color: UIColor) {
        let anim = CABasicAnimation.init(keyPath: #keyPath(CALayer.backgroundColor))
        anim.fromValue = UIColor.clear.cgColor
        anim.toValue = color.cgColor
        anim.duration = 0.5
        anim.autoreverses = true
        anim.repeatCount = 0
        anim.fillMode = kCAFillModeForwards
        anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        self.layer.add(anim, forKey: "blink")
    }

    func bounce() {
        let endTransform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        let prepareTransform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        UIView.animateKeyframes(
            withDuration: 0.2, delay: 0.0, options: UIView.KeyframeAnimationOptions.calculationModeLinear, animations: {
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.1) {
                    self.transform = prepareTransform
                }
                UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.1) {
                    self.transform = endTransform
                }
        }, completion: nil)
    }
}

extension BounceButton {
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.bounce()
        super.touchesBegan(touches, with: event)
    }
}

但在你的情况下,你可以为所有 UIView 扩展做,所以一切都可以反弹。

于 2019-07-19T10:51:31.530 回答