From 87a214995844b6c88f3c019bb084d8e96790c51c Mon Sep 17 00:00:00 2001 From: Robert Doan Date: Wed, 25 Aug 2021 10:53:57 +0700 Subject: [PATCH] feat: Add removeHighlightedWhenTouchReleased --- .../Demos/LineChart1ViewController.swift | 1 + .../Charts/Charts/BarLineChartViewBase.swift | 37 +++++++++++++++++++ Source/Charts/Utils/Platform+Gestures.swift | 1 + 3 files changed, 39 insertions(+) diff --git a/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift b/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift index e676cd9e1a..1194082f23 100644 --- a/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift +++ b/ChartsDemo-iOS/Swift/Demos/LineChart1ViewController.swift @@ -45,6 +45,7 @@ class LineChart1ViewController: DemoBaseViewController { chartView.dragEnabled = true chartView.setScaleEnabled(true) chartView.pinchZoomEnabled = true + chartView.removeHighlightedWhenTouchReleased = true // x-axis limit line let llXAxis = ChartLimitLine(limit: 10, label: "Index 10") diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index 631ccd07d9..fd09c88555 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -39,6 +39,13 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD /// the color for the background of the chart-drawing area (everything behind the grid lines). @objc open var gridBackgroundColor = NSUIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1.0) + @objc open var removeHighlightedWhenTouchReleased = false { + didSet { + _tapGestureRecognizer.isEnabled = !removeHighlightedWhenTouchReleased; + _longPressGestureRecognizer.isEnabled = removeHighlightedWhenTouchReleased; + } + } + @objc open var borderColor = NSUIColor.black @objc open var borderLineWidth: CGFloat = 1.0 @@ -88,6 +95,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD @objc open lazy var xAxisRenderer = XAxisRenderer(viewPortHandler: viewPortHandler, axis: xAxis, transformer: _leftAxisTransformer) internal var _tapGestureRecognizer: NSUITapGestureRecognizer! + internal var _longPressGestureRecognizer: NSUILongPressGestureRecognizer! internal var _doubleTapGestureRecognizer: NSUITapGestureRecognizer! #if !os(tvOS) internal var _pinchGestureRecognizer: NSUIPinchGestureRecognizer! @@ -122,6 +130,8 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD self.highlighter = ChartHighlighter(chart: self) _tapGestureRecognizer = NSUITapGestureRecognizer(target: self, action: #selector(tapGestureRecognized(_:))) + _longPressGestureRecognizer = NSUILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognized(_:))) + _longPressGestureRecognizer.minimumPressDuration = 0 _doubleTapGestureRecognizer = NSUITapGestureRecognizer(target: self, action: #selector(doubleTapGestureRecognized(_:))) _doubleTapGestureRecognizer.nsuiNumberOfTapsRequired = 2 _panGestureRecognizer = NSUIPanGestureRecognizer(target: self, action: #selector(panGestureRecognized(_:))) @@ -129,9 +139,12 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD _panGestureRecognizer.delegate = self self.addGestureRecognizer(_tapGestureRecognizer) + self.addGestureRecognizer(_longPressGestureRecognizer) self.addGestureRecognizer(_doubleTapGestureRecognizer) self.addGestureRecognizer(_panGestureRecognizer) + _tapGestureRecognizer.isEnabled = !removeHighlightedWhenTouchReleased; + _longPressGestureRecognizer.isEnabled = removeHighlightedWhenTouchReleased; _doubleTapGestureRecognizer.isEnabled = _doubleTapToZoomEnabled _panGestureRecognizer.isEnabled = _dragXEnabled || _dragYEnabled @@ -555,6 +568,30 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD } } + @objc private func longPressGestureRecognized(_ recognizer: NSUITapGestureRecognizer) + { + if data === nil + { + return + } + + if recognizer.state == NSUIGestureRecognizerState.began || + recognizer.state == NSUIGestureRecognizerState.changed + { + if !isHighLightPerTapEnabled { return } + + let highlight = getHighlightByTouchPoint(recognizer.location(in: self)) + + if highlight != self.lastHighlighted { + lastHighlighted = highlight + highlightValue(highlight, callDelegate: true) + } + } else if recognizer.state == NSUIGestureRecognizerState.ended { + highlightValue(nil, callDelegate: true) + setNeedsDisplay() + } + } + @objc private func doubleTapGestureRecognized(_ recognizer: NSUITapGestureRecognizer) { if data === nil diff --git a/Source/Charts/Utils/Platform+Gestures.swift b/Source/Charts/Utils/Platform+Gestures.swift index e22e168876..18b36127d4 100644 --- a/Source/Charts/Utils/Platform+Gestures.swift +++ b/Source/Charts/Utils/Platform+Gestures.swift @@ -18,6 +18,7 @@ public typealias NSUIGestureRecognizerState = UIGestureRecognizer.State public typealias NSUIGestureRecognizerDelegate = UIGestureRecognizerDelegate public typealias NSUITapGestureRecognizer = UITapGestureRecognizer public typealias NSUIPanGestureRecognizer = UIPanGestureRecognizer +public typealias NSUILongPressGestureRecognizer = UILongPressGestureRecognizer extension NSUITapGestureRecognizer {