Skip to content

Commit

Permalink
add double tap support
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Jun 10, 2022
1 parent 5f80877 commit d744193
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Sources/UIComponent/Components/View/TappableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sources/UIComponent/Core/ComponentView/ComponentEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions Sources/UIComponent/Core/Model/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit d744193

Please sign in to comment.