Skip to content

Commit

Permalink
make TappableView support long press
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Aug 3, 2021
1 parent 7c1c7a9 commit 36974bd
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions Sources/UIComponent/Components/TappableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,30 @@ public struct TappableViewConfiguration {
}

open class TappableView: ComponentView {
public var onTap: ((TappableView) -> Void)?
public var configuration: TappableViewConfiguration?

lazy var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap))
lazy var longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))

public var onTap: ((TappableView) -> Void)? {
didSet {
if onTap != nil {
addGestureRecognizer(tapGestureRecognizer)
} else {
removeGestureRecognizer(tapGestureRecognizer)
}
}
}

public var onLongPress: ((TappableView) -> Void)? {
didSet {
if onLongPress != nil {
addGestureRecognizer(longPressGestureRecognizer)
} else {
removeGestureRecognizer(longPressGestureRecognizer)
}
}
}

open var isHighlighted: Bool = false {
didSet {
Expand All @@ -37,7 +59,6 @@ open class TappableView: ComponentView {
public override init(frame: CGRect) {
super.init(frame: frame)
accessibilityTraits = .button
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTap)))
}

required public init?(coder: NSCoder) {
Expand All @@ -64,6 +85,12 @@ open class TappableView: ComponentView {
config.didTap?(self)
onTap?(self)
}

@objc open func didLongPress() {
if longPressGestureRecognizer.state == .began {
onLongPress?(self)
}
}
}

extension Component {
Expand Down

0 comments on commit 36974bd

Please sign in to comment.