Skip to content

Instantly share code, notes, and snippets.

@saggy
Forked from FokkeZB/app.js
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saggy/663595cd3db37b20a174 to your computer and use it in GitHub Desktop.
Save saggy/663595cd3db37b20a174 to your computer and use it in GitHub Desktop.
var v = Ti.UI.createView();
var s = new require('spinner').Spinner(
v, // View to spin
30 // Degrees to spin per millisecond
); // Auto-starts
// Stop
s.stop();
// Start
s.start();
// Change speed
s.start(20);
function Spinner(view, _speed) {
var speed = _speed || 5,
timeout,
spinning = false,
degrees = 0;
var self = this;
function rotate() {
var t = Ti.UI.create2DMatrix();
degrees = degrees + speed;
t = t.rotate(degrees);
var a = Titanium.UI.createAnimation();
a.transform = t;
a.duration = 1;
view.animate(a);
loop();
}
function loop() {
if (spinning === true) {
timeout = setTimeout(rotate, 1);
}
}
this.stop = function () {
if (spinning === true) {
spinning = false;
timeout && clearTimeout(timeout);
}
return this;
}
this.start = function (_speed) {
if (spinning === false || (_speed && _speed != speed)) {
if (_speed) {
speed = _speed;
this.stop();
}
spinning = true;
loop();
}
return this;
}
return this.start();
}
exports.Spinner = Spinner;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment