Skip to content

Instantly share code, notes, and snippets.

@alexshive
Forked from FokkeZB/app.js
Last active December 17, 2015 23:18
Show Gist options
  • Select an option

  • Save alexshive/5687959 to your computer and use it in GitHub Desktop.

Select an option

Save alexshive/5687959 to your computer and use it in GitHub Desktop.
Didn't work on fresh Titanium project before and was too fast spinning. Fixed for demonstration purposes.
var win = Ti.UI.createWindow({ backgroundColor: '#000' });
var v = Ti.UI.createView({ width: 50, height: 50, backgroundColor: '#fff'});
var s = 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(100);
win.add(v);
win.open();
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