Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active December 16, 2015 12:28
Show Gist options
  • Save FokkeZB/5434416 to your computer and use it in GitHub Desktop.
Save FokkeZB/5434416 to your computer and use it in GitHub Desktop.
CommonJS module to spin a view in Titanium
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