From 65bf8b38aec26b53ada987737bc2f8816250fd1d Mon Sep 17 00:00:00 2001 From: Muukii Date: Wed, 25 Jan 2023 17:35:07 +0900 Subject: [PATCH] use normal node for interactive-node --- .../Compositions/InteractiveNode.swift | 66 ++++++++++++------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/Sources/Components/Compositions/InteractiveNode.swift b/Sources/Components/Compositions/InteractiveNode.swift index 4c84a7d..1d0b9a1 100644 --- a/Sources/Components/Compositions/InteractiveNode.swift +++ b/Sources/Components/Compositions/InteractiveNode.swift @@ -27,7 +27,7 @@ import Descriptors A display node that supports interactions tap and long-press. According to that, animations are also supported. */ -open class InteractiveNode: NamedDisplayControlNodeBase { +open class InteractiveNode: NamedDisplayNodeBase { public typealias BodyNode = D @@ -119,18 +119,6 @@ open class InteractiveNode: NamedDisplayControlNodeBase { self.highlightHandler = context.handler - addTarget( - self, - action: #selector(_touchUpInside), - forControlEvents: .touchUpInside - ) - - addTarget( - self, - action: #selector(_touchDownInside), - forControlEvents: .touchDown - ) - if useLongPressGesture { let longPressGesture = UILongPressGestureRecognizer( target: self, @@ -151,22 +139,47 @@ open class InteractiveNode: NamedDisplayControlNodeBase { } } - open override var isHighlighted: Bool { - didSet { - highlightHandler?(isHighlighted, self.view, animationTargetNode.view) - } - } - @MainActor - @objc private func _touchDownInside() { + public override func touchesBegan(_ touches: Set, with event: UIEvent?) { + + super.touchesBegan(touches, with: event) + + highlightHandler?(true, self.view, animationTargetNode.view) haptics?.send(event: .onTouchDownInside) } - + @MainActor - @objc private func _touchUpInside() { + public override func touchesEnded(_ touches: Set, with event: UIEvent?) { + + super.touchesEnded(touches, with: event) + + highlightHandler?(false, self.view, animationTargetNode.view) + + guard !shouldCancelTouches(touches) else { + return + } + haptics?.send(event: .onTouchUpInside) handlers.onTap() } + + @MainActor + public override func touchesMoved(_ touches: Set, with event: UIEvent?) { + + super.touchesMoved(touches, with: event) + + if shouldCancelTouches(touches) { + highlightHandler?(false, self.view, animationTargetNode.view) + } else { + highlightHandler?(true, self.view, animationTargetNode.view) + } + } + + @MainActor + public override func touchesCancelled(_ touches: Set?, with event: UIEvent?) { + super.touchesCancelled(touches, with: event) + highlightHandler?(false, self.view, animationTargetNode.view) + } @MainActor @objc private func _onLongPress(_ gesture: UILongPressGestureRecognizer) { @@ -188,4 +201,13 @@ open class InteractiveNode: NamedDisplayControlNodeBase { handlers.onLongPress = handler return self } + + private func shouldCancelTouches(_ touches: Set) -> Bool { + guard + let touch = touches.first, + bounds.insetBy(dx: -50, dy: -50).contains(touch.location(in: self.view)) else { + return true + } + return false + } }