Skip to content

Instantly share code, notes, and snippets.

@alanleard
Created October 11, 2011 16:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save alanleard/1278595 to your computer and use it in GitHub Desktop.
Save alanleard/1278595 to your computer and use it in GitHub Desktop.
PageFlip with Pinch/Zoom
var win = Ti.UI.currentWindow;
Titanium.PageFlip = Ti.PageFlip = require('ti.pageflip');
var pdf = 'http://assets.appcelerator.com.s3.amazonaws.com/docs/Appcelerator-IDC-Q1-2011-Mobile-Developer-Report.pdf';
var fileName = pdf.split('/').pop();
var pdfFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, fileName);
function downloadPDF() {
var progressBar = Ti.UI.createProgressBar({ max: 1, min: 0, value: 0, visible: true });
win.add(progressBar);
var xhr = Ti.Network.createHTTPClient({
ondatastream: function(e) {
progressBar.value = e.progress;
},
onreadystatechange: function() {
if (xhr.readyState == 4) {
pdfFile.write(this.responseData);
win.remove(progressBar);
showPDF();
}
}
});
xhr.open('GET', pdf);
xhr.send();
}
function showPDF() {
/**
* "createView" will return a pageflip view. It can be sized and positioned like any other Titanium view. Here we
* call it with a pdf and a transition; it will handle paging the PDF for us.
*/
var scrollView = Ti.UI.createScrollView({
zoomScale:1.0,
minZoomScale:1.0,
maxZoomScale:4.0
});
var pageflip = Ti.PageFlip.createView({
transition: Ti.PageFlip.TRANSITION_FLIP, /* All Options: TRANSITION_FLIP [default], TRANSITION_SLIDE, TRANSITION_FADE */
pdf: pdfFile.nativePath,
tapToAdvanceWidth: '15%'
});
scrollView.add(pageflip);
win.add(scrollView);
scrollView.addEventListener('doubletap', function(){
scrollView.zoomScale = 1.0;
})
function updateWindowTitle(evt) {
win.title = 'PDF, 1 < ' + (evt.currentPage+1) + ' > ' + evt.pageCount;
}
updateWindowTitle(pageflip);
pageflip.addEventListener('change', function(evt) {
scrollView.zoomScale = 1.0;
updateWindowTitle(evt);
});
}
if (pdfFile.exists()) {
showPDF();
}
else {
downloadPDF();
}
@chmiiller
Copy link

works fine! Thanks =]

@trd-isaac
Copy link

Thanks, works great!

@mpmccormick
Copy link

Where are you putting this code? I'm new to titanium and just learning the platform ... I'm using the latest Titanium Studio and 3.x SDK...

I created a new Classic mobile app and put this in app.js to try it out ... but after my new window().open() call, I keep getting an error:

'undefined' is not an object (evaluating win.add)

Any help would be appreciated.

TIA
mcMpM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment