Getting to the point, I have a scrollable view. The code is below. I know I can set the scrollview to not take touch events, that much is easy, but what I need to find is how to make it swap views on a timer, say, 5 seconds.
Thanks
var scrollView = Titanium.UI.createScrollableView({ showPagingControl : true, }); var viewArray = []; for(var i = 1; i <= 5; i++) { var couponView = Ti.UI.createView({ top : '55dp', width : Ti.Platform.displayCaps.platformWidth, height : '150dp', }); var mainButton = Ti.UI.createButton({ backgroundImage : '/app/images/coupon.png', top : '0dp', width : '282dp', height : '150dp', }); couponView.add(mainButton); viewArray.push(couponView); } scrollView.views = viewArray; mainMenu.add(scrollView);
3 Answers
Accepted Answer
Ok if you are using iOS you can do scrollingEnabled = false to stop the user from scrolling, on android you might need to overwrite the events.
On the timer do a setInterval to execute the moveNext() function. (this should work for both os)
setInterval(function() { sc.moveNext(); }, 5000);
Solution:
function startScrolling() { for(; ; ) { sleep(6000); Ti.API.info('Scrolling To Index: ' + viewIndex); scrollView.scrollToView(viewIndex); if(viewIndex == 4) { viewIndex = 0; scrollView.scrollToView(viewIndex); } else { scrollView.scrollToView(viewIndex); viewIndex++; } } } function sleep(milliseconds) { var start = new Date().getTime(); while((new Date().getTime() - start) < milliseconds) { // Do nothing } }
Here is a small example
var count = 0; var timer = setInterval(function(){ count++; label.text = "count: " + count; if (count == 10) { clearInterval(timer);//<<<<<<<<---used this to clear the timer } }, 1000);
Your Answer
Think you can help? Login to answer this question!