Skip to content

Instantly share code, notes, and snippets.

@joacim-boive
Created July 18, 2011 18:13
Show Gist options
  • Save joacim-boive/1090202 to your computer and use it in GitHub Desktop.
Save joacim-boive/1090202 to your computer and use it in GitHub Desktop.
Indefinite swipe:ing of views using Appcelerator Titanium.
/*
The idea is to just use 2 views that can hold whatever (in my case images).
Swipe:ing left/right will move the views that is currently hidden accordingly, making it seem like it's an infinite number of views at any direction.
This should be a very memory efficient approach.
The code below requires Appcelerator Titanium: http://appcelerator.com
*/
var win = Ti.UI.createWindow(),
animate = Ti.UI.createAnimation({
curve: Ti.UI.ANIMATION_CURVE_EASE_OUT,
duration: 1500
}),
vWindow = Ti.UI.createView({
left: 0,
width: 640
})
animate.addEventListener('complete', function() {
vWindow.myIndex = (vWindow.myIndex ? 0 : 1);
var vThis = vWindow.myViews[vWindow.myIndex],
vPrevious = vWindow.myViews[vWindow.myIndex ? 0 : 1];
vWindow.myIsType = '';
vWindow.right = null;
vWindow.left = 0;
vPrevious.right = null;
vPrevious.left = 320;
vThis.right = null;
vThis.left = 0;
});
vWindow.myViews = [
Ti.UI.createView({
left: 0,
width: 320,
backgroundColor: 'white',
borderColor: 'green',
borderWidth: 5
}),
Ti.UI.createView({
left: 320,
width: 320,
backgroundColor: 'red',
borderColor: 'yellow',
borderWidth: 5
})
];
vWindow.myIndex = 0;
vWindow.myIsType = '';
vWindow.add(vWindow.myViews);
vWindow.addEventListener('swipe', doShow);
win.add(vWindow);
win.open();
function doShow(e) {
var direction = e.direction,
index = (vWindow.myIndex ? 0 : 1),
views = vWindow.myViews,
toView = views[index];
if(vWindow.myIsType == '') {
vWindow.myIsType = e.type;
if(direction == 'left' || direction == 'right') {
if(direction == 'left') {
animate.left = null;
animate.right = 320;
toView.left = 320;
} else {
animate.right = null;
animate.left = 320;
toView.left = -320;
}
vWindow.animate(animate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment