Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Mar 20, 2015
1 parent c607aed commit 19165dd
Show file tree
Hide file tree
Showing 155 changed files with 21,267 additions and 0 deletions.
626 changes: 626 additions & 0 deletions Charts/Charts.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

149 changes: 149 additions & 0 deletions Charts/Classes/Animation/ChartAnimator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// ChartAnimator.swift
// Charts
//
// Created by Daniel Cohen Gindi on 3/3/15.
//
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
// A port of MPAndroidChart for iOS
// Licensed under Apache License 2.0
//
// https://github.com/danielgindi/ios-charts
//

import Foundation
import UIKit

@objc
public protocol ChartAnimatorDelegate
{
// Called when the Animator has stepped.
func chartAnimatorUpdated(chartAnimator: ChartAnimator);
}

public class ChartAnimator: NSObject
{
public weak var delegate: ChartAnimatorDelegate?;

/// the phase that is animated and influences the drawn values on the y-axis
public var phaseX: CGFloat = 1.0

/// the phase that is animated and influences the drawn values on the y-axis
public var phaseY: CGFloat = 1.0

private var _startTime: NSTimeInterval = 0.0
private var _displayLink: CADisplayLink!
private var _endTimeX: NSTimeInterval = 0.0
private var _endTimeY: NSTimeInterval = 0.0
private var _endTime: NSTimeInterval = 0.0
private var _enabledX: Bool = false
private var _enabledY: Bool = false

public override init()
{
super.init();
}

deinit
{
stop();
}

public func stop()
{
if (_displayLink != nil)
{
_displayLink.removeFromRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes);
_displayLink = nil;

_enabledX = false;
_enabledY = false;
}
}

@objc private func animationLoop()
{
var currentTime = CACurrentMediaTime();
if (_enabledX)
{
var duration = _endTimeX - _startTime;
phaseX = duration == 0.0 ? 0.0 : CGFloat((currentTime - _startTime) / duration);
if (phaseX > 1.0)
{
phaseX = 1.0;
}
}
if (_enabledY)
{
var duration = _endTimeY - _startTime;
phaseY = duration == 0.0 ? 0.0 : CGFloat((currentTime - _startTime) / duration);
if (phaseY > 1.0)
{
phaseY = 1.0;
}
}
if (currentTime >= _endTime)
{
stop();
}
if (delegate != nil)
{
delegate!.chartAnimatorUpdated(self);
}
}

/// 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.
public func animateXY(#durationX: NSTimeInterval, durationY: NSTimeInterval)
{
stop();

_displayLink = CADisplayLink(target: self, selector: Selector("animationLoop"));

_startTime = CACurrentMediaTime();
_endTimeX = _startTime + durationX;
_endTimeY = _startTime + durationY;
_endTime = _endTimeX > _endTimeY ? _endTimeX : _endTimeY;
_enabledX = true;
_enabledY = true;

_displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes);
}

/// 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.
public func animateX(#duration: NSTimeInterval)
{
stop();

_displayLink = CADisplayLink(target: self, selector: Selector("animationLoop"));

_startTime = CACurrentMediaTime();
_endTimeX = _startTime + duration;
_endTime = _endTimeX;
_enabledX = true;
_enabledY = false;

_displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes);
}

/// 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.
public func animateY(#duration: NSTimeInterval)
{
stop();

_displayLink = CADisplayLink(target: self, selector: Selector("animationLoop"));

_startTime = CACurrentMediaTime();
_endTimeY = _startTime + duration;
_endTime = _endTimeY;
_enabledX = false;
_enabledY = true;

_displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes);
}
}
Loading

0 comments on commit 19165dd

Please sign in to comment.