Skip to content

Commit

Permalink
Merge pull request #3005 from jjatie/animator-cleanup
Browse files Browse the repository at this point in the history
Minor changes to Animator
  • Loading branch information
pmairoldi committed Nov 17, 2017
2 parents c37e553 + 50fdd2d commit 19e8b00
Showing 1 changed file with 22 additions and 60 deletions.
82 changes: 22 additions & 60 deletions Source/Charts/Animation/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ open class Animator: NSObject

fileprivate var _easingX: ChartEasingFunctionBlock?
fileprivate var _easingY: ChartEasingFunctionBlock?

public override init()
{
super.init()
Expand All @@ -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
Expand All @@ -91,7 +90,7 @@ open class Animator: NSObject
stopBlock?()
}

fileprivate func updateAnimationPhases(_ currentTime: TimeInterval)
private func updateAnimationPhases(_ currentTime: TimeInterval)
{
if _enabledX
{
Expand All @@ -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
Expand All @@ -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
}
}

Expand All @@ -134,7 +126,6 @@ open class Animator: NSObject
updateAnimationPhases(currentTime)

delegate?.animatorUpdated(self)

updateBlock?()

if currentTime >= _endTime
Expand Down Expand Up @@ -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
Expand All @@ -233,33 +215,23 @@ 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)
}
}

/// 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
/// - 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
Expand All @@ -277,30 +249,20 @@ 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)
}
}

/// 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
/// - 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)
}
}

0 comments on commit 19e8b00

Please sign in to comment.