From 61f88e26863fd8bfa1b7255b82c2e335564b0840 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Thu, 23 Nov 2017 14:36:48 -0500 Subject: [PATCH] Minor logic cleanup --- Source/Charts/Jobs/AnimatedMoveViewJob.swift | 22 --------- Source/Charts/Jobs/AnimatedViewPortJob.swift | 50 ++++++++------------ Source/Charts/Jobs/MoveViewJob.swift | 4 +- Source/Charts/Jobs/ZoomViewJob.swift | 6 +-- 4 files changed, 24 insertions(+), 58 deletions(-) diff --git a/Source/Charts/Jobs/AnimatedMoveViewJob.swift b/Source/Charts/Jobs/AnimatedMoveViewJob.swift index b2dc5d2205..b6ea8c28e5 100644 --- a/Source/Charts/Jobs/AnimatedMoveViewJob.swift +++ b/Source/Charts/Jobs/AnimatedMoveViewJob.swift @@ -18,28 +18,6 @@ import CoreGraphics open class AnimatedMoveViewJob: AnimatedViewPortJob { - public override init( - viewPortHandler: ViewPortHandler, - xValue: Double, - yValue: Double, - transformer: Transformer, - view: ChartViewBase, - xOrigin: CGFloat, - yOrigin: CGFloat, - duration: TimeInterval, - easing: ChartEasingFunctionBlock?) - { - super.init(viewPortHandler: viewPortHandler, - xValue: xValue, - yValue: yValue, - transformer: transformer, - view: view, - xOrigin: xOrigin, - yOrigin: yOrigin, - duration: duration, - easing: easing) - } - internal override func animationUpdate() { guard diff --git a/Source/Charts/Jobs/AnimatedViewPortJob.swift b/Source/Charts/Jobs/AnimatedViewPortJob.swift index dbde0bf4b1..d0bc9f84f9 100644 --- a/Source/Charts/Jobs/AnimatedViewPortJob.swift +++ b/Source/Charts/Jobs/AnimatedViewPortJob.swift @@ -71,49 +71,37 @@ open class AnimatedViewPortJob: ViewPortJob updateAnimationPhase(_startTime) _displayLink = NSUIDisplayLink(target: self, selector: #selector(animationLoop)) - _displayLink.add(to: RunLoop.main, forMode: RunLoopMode.commonModes) + _displayLink.add(to: .main, forMode: .commonModes) } @objc open func stop(finish: Bool) { - if _displayLink != nil + guard _displayLink != nil else { return } + + _displayLink.remove(from: .main, forMode: .commonModes) + _displayLink = nil + + if finish { - _displayLink.remove(from: RunLoop.main, forMode: RunLoopMode.commonModes) - _displayLink = nil - - if finish + if phase != 1.0 { - if phase != 1.0 - { - phase = 1.0 - phase = 1.0 - - animationUpdate() - } - - animationEnd() + phase = 1.0 + animationUpdate() } + + animationEnd() } } fileprivate func updateAnimationPhase(_ currentTime: TimeInterval) { - let elapsedTime: TimeInterval = currentTime - _startTime - let duration: TimeInterval = _duration - var elapsed: TimeInterval = elapsedTime - if elapsed > duration - { - elapsed = duration - } - - if _easing != nil - { - phase = CGFloat(_easing!(elapsed, duration)) - } - else - { - phase = CGFloat(elapsed / duration) - } + let elapsedTime = currentTime - _startTime + let duration = _duration + var elapsed = elapsedTime + + elapsed = min(elapsed, duration) + + phase = CGFloat(_easing?(elapsed, duration) ?? elapsed / duration) } @objc fileprivate func animationLoop() diff --git a/Source/Charts/Jobs/MoveViewJob.swift b/Source/Charts/Jobs/MoveViewJob.swift index b175b1707e..c96834edaa 100644 --- a/Source/Charts/Jobs/MoveViewJob.swift +++ b/Source/Charts/Jobs/MoveViewJob.swift @@ -43,8 +43,8 @@ open class MoveViewJob: ViewPortJob else { return } var pt = CGPoint( - x: CGFloat(xValue), - y: CGFloat(yValue) + x: xValue, + y: yValue ) transformer.pointValueToPixel(&pt) diff --git a/Source/Charts/Jobs/ZoomViewJob.swift b/Source/Charts/Jobs/ZoomViewJob.swift index 7a6c8d8911..b9880cc600 100644 --- a/Source/Charts/Jobs/ZoomViewJob.swift +++ b/Source/Charts/Jobs/ZoomViewJob.swift @@ -21,7 +21,7 @@ open class ZoomViewJob: ViewPortJob { @objc internal var scaleX: CGFloat = 0.0 @objc internal var scaleY: CGFloat = 0.0 - @objc internal var axisDependency: YAxis.AxisDependency = YAxis.AxisDependency.left + @objc internal var axisDependency: YAxis.AxisDependency = .left @objc public init( viewPortHandler: ViewPortHandler, @@ -60,8 +60,8 @@ open class ZoomViewJob: ViewPortJob let xValsInView = (view as! BarLineChartViewBase).xAxis.axisRange / Double(viewPortHandler.scaleX) var pt = CGPoint( - x: CGFloat(xValue - xValsInView / 2.0), - y: CGFloat(yValue + yValsInView / 2.0) + x: CGFloat(xValue - xValsInView) / 2.0, + y: CGFloat(yValue + yValsInView) / 2.0 ) transformer.pointValueToPixel(&pt)