Last active
December 16, 2015 12:28
-
-
Save FokkeZB/5434416 to your computer and use it in GitHub Desktop.
CommonJS module to spin a view in Titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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