From d744193925826ab4c67aa08574d9a67e101e3a4e Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Fri, 10 Jun 2022 11:09:43 -0700 Subject: [PATCH] add double tap support --- .../Components/View/TappableView.swift | 18 ++++++++++++++++++ .../Core/ComponentView/ComponentEngine.swift | 4 ++-- Sources/UIComponent/Core/Model/Animator.swift | 6 +----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Sources/UIComponent/Components/View/TappableView.swift b/Sources/UIComponent/Components/View/TappableView.swift index d1d0190f..12bb394c 100644 --- a/Sources/UIComponent/Components/View/TappableView.swift +++ b/Sources/UIComponent/Components/View/TappableView.swift @@ -21,6 +21,9 @@ open class TappableView: ComponentView { public var configuration: TappableViewConfiguration? lazy var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap)) + lazy var doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap)).then { + $0.numberOfTapsRequired = 2 + } lazy var longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress)) lazy var contextMenuInteraction = UIContextMenuInteraction(delegate: self) @@ -45,6 +48,17 @@ open class TappableView: ComponentView { } } + public var onDoubleTap: ((TappableView) -> Void)? { + didSet { + if onDoubleTap != nil { + tapGestureRecognizer.require(toFail: doubleTapGestureRecognizer) + addGestureRecognizer(doubleTapGestureRecognizer) + } else { + removeGestureRecognizer(doubleTapGestureRecognizer) + } + } + } + private var dropInteraction: UIDropInteraction? public weak var dropDelegate: UIDropInteractionDelegate? { didSet { @@ -130,6 +144,10 @@ open class TappableView: ComponentView { onTap?(self) } + @objc open func didDoubleTap() { + onDoubleTap?(self) + } + @objc open func didLongPress() { if longPressGestureRecognizer.state == .began { onLongPress?(self) diff --git a/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift b/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift index ce09cfda..d10bcafe 100644 --- a/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift +++ b/Sources/UIComponent/Core/ComponentView/ComponentEngine.swift @@ -227,6 +227,7 @@ public class ComponentEngine { newViews[index] = cell } else { let animator = visibleRenderable[index].animator ?? animator + animator.shift(componentView: componentView, delta: contentOffsetDelta, view: cell) animator.delete(componentView: componentView, view: cell) { cell.recycleForUIComponentReuse() } @@ -244,8 +245,7 @@ public class ComponentEngine { if updateViews { // view was on screen before reload, need to update the view. viewData.renderNode._updateView(view) - (viewData.animator ?? animator).shift(componentView: componentView, delta: contentOffsetDelta, - view: view, frame: frame) + (viewData.animator ?? animator).shift(componentView: componentView, delta: contentOffsetDelta, view: view) } } else { view = viewData.renderNode._makeView() as! UIView diff --git a/Sources/UIComponent/Core/Model/Animator.swift b/Sources/UIComponent/Core/Model/Animator.swift index 9e36661a..d48d4a7d 100644 --- a/Sources/UIComponent/Core/Model/Animator.swift +++ b/Sources/UIComponent/Core/Model/Animator.swift @@ -72,11 +72,7 @@ open class Animator { /// - componentView: source ComponentView /// - delta: changes in contentOffset /// - view: the view being updated - /// - frame: frame provided by the layout - open func shift(componentView: ComponentDisplayableView, - delta: CGPoint, - view: UIView, - frame: CGRect) { + open func shift(componentView: ComponentDisplayableView, delta: CGPoint, view: UIView) { view.center += delta } }