diff --git a/Source/Charts/Animation/Animator.swift b/Source/Charts/Animation/Animator.swift index cec0987572..f5d8459095 100644 --- a/Source/Charts/Animation/Animator.swift +++ b/Source/Charts/Animation/Animator.swift @@ -55,7 +55,7 @@ open class Animator: NSObject fileprivate var _easingX: ChartEasingFunctionBlock? fileprivate var _easingY: ChartEasingFunctionBlock? - + public override init() { super.init() @@ -68,11 +68,10 @@ open class Animator: NSObject @objc open func stop() { - guard let _displayLink = _displayLink else { - return - } - _displayLink.remove(from: RunLoop.main, forMode: RunLoopMode.commonModes) - self._displayLink = nil + guard _displayLink != nil else { return } + + _displayLink?.remove(from: .main, forMode: .commonModes) + _displayLink = nil _enabledX = false _enabledY = false @@ -91,7 +90,7 @@ open class Animator: NSObject stopBlock?() } - fileprivate func updateAnimationPhases(_ currentTime: TimeInterval) + private func updateAnimationPhases(_ currentTime: TimeInterval) { if _enabledX { @@ -103,7 +102,7 @@ open class Animator: NSObject elapsed = duration } - phaseX = _easingX?(elapsed, duration) ?? Double(elapsed / duration) + phaseX = _easingX?(elapsed, duration) ?? elapsed / duration } if _enabledY @@ -115,15 +114,8 @@ open class Animator: NSObject { elapsed = duration } - - if let _easingY = _easingY - { - phaseY = _easingY(elapsed, duration) - } - else - { - phaseY = Double(elapsed / duration) - } + + phaseY = _easingY?(elapsed, duration) ?? elapsed / duration } } @@ -134,7 +126,6 @@ open class Animator: NSObject updateAnimationPhases(currentTime) delegate?.animatorUpdated(self) - updateBlock?() if currentTime >= _endTime @@ -202,20 +193,11 @@ open class Animator: NSObject /// - parameter xAxisDuration: duration for animating the x axis /// - parameter yAxisDuration: duration for animating the y axis /// - parameter easingOption: the easing function for the animation - @objc open func animate(xAxisDuration: TimeInterval, yAxisDuration: TimeInterval, easingOption: ChartEasingOption) + @objc open func animate(xAxisDuration: TimeInterval, yAxisDuration: TimeInterval, easingOption: ChartEasingOption = .easeInOutSine) { animate(xAxisDuration: xAxisDuration, yAxisDuration: yAxisDuration, easing: easingFunctionFromOption(easingOption)) } - - /// Animates the drawing / rendering of the chart on both x- and y-axis with the specified animation time. - /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. - /// - parameter xAxisDuration: duration for animating the x axis - /// - parameter yAxisDuration: duration for animating the y axis - @objc open func animate(xAxisDuration: TimeInterval, yAxisDuration: TimeInterval) - { - animate(xAxisDuration: xAxisDuration, yAxisDuration: yAxisDuration, easingOption: .easeInOutSine) - } - + /// Animates the drawing / rendering of the chart the x-axis with the specified animation time. /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. /// - parameter xAxisDuration: duration for animating the x axis @@ -233,13 +215,11 @@ open class Animator: NSObject // Take care of the first frame if rendering is already scheduled... updateAnimationPhases(_startTimeX) - if _enabledX || _enabledY + if _enabledX || _enabledY, + _displayLink == nil { - if _displayLink == nil - { - _displayLink = NSUIDisplayLink(target: self, selector: #selector(animationLoop)) - _displayLink?.add(to: RunLoop.main, forMode: RunLoopMode.commonModes) - } + _displayLink = NSUIDisplayLink(target: self, selector: #selector(animationLoop)) + _displayLink?.add(to: .main, forMode: .commonModes) } } @@ -247,19 +227,11 @@ open class Animator: NSObject /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. /// - parameter xAxisDuration: duration for animating the x axis /// - parameter easingOption: the easing function for the animation - @objc open func animate(xAxisDuration: TimeInterval, easingOption: ChartEasingOption) + @objc open func animate(xAxisDuration: TimeInterval, easingOption: ChartEasingOption = .easeInOutSine) { animate(xAxisDuration: xAxisDuration, easing: easingFunctionFromOption(easingOption)) } - - /// Animates the drawing / rendering of the chart the x-axis with the specified animation time. - /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. - /// - parameter xAxisDuration: duration for animating the x axis - @objc open func animate(xAxisDuration: TimeInterval) - { - animate(xAxisDuration: xAxisDuration, easingOption: .easeInOutSine) - } - + /// Animates the drawing / rendering of the chart the y-axis with the specified animation time. /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. /// - parameter yAxisDuration: duration for animating the y axis @@ -277,13 +249,11 @@ open class Animator: NSObject // Take care of the first frame if rendering is already scheduled... updateAnimationPhases(_startTimeY) - if _enabledX || _enabledY + if _enabledX || _enabledY, + _displayLink == nil { - if _displayLink == nil - { - _displayLink = NSUIDisplayLink(target: self, selector: #selector(animationLoop)) - _displayLink?.add(to: RunLoop.main, forMode: RunLoopMode.commonModes) - } + _displayLink = NSUIDisplayLink(target: self, selector: #selector(animationLoop)) + _displayLink?.add(to: .main, forMode: .commonModes) } } @@ -291,16 +261,8 @@ open class Animator: NSObject /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. /// - parameter yAxisDuration: duration for animating the y axis /// - parameter easingOption: the easing function for the animation - @objc open func animate(yAxisDuration: TimeInterval, easingOption: ChartEasingOption) + @objc open func animate(yAxisDuration: TimeInterval, easingOption: ChartEasingOption = .easeInOutSine) { animate(yAxisDuration: yAxisDuration, easing: easingFunctionFromOption(easingOption)) } - - /// Animates the drawing / rendering of the chart the y-axis with the specified animation time. - /// If `animate(...)` is called, no further calling of `invalidate()` is necessary to refresh the chart. - /// - parameter yAxisDuration: duration for animating the y axis - @objc open func animate(yAxisDuration: TimeInterval) - { - animate(yAxisDuration: yAxisDuration, easingOption: .easeInOutSine) - } }