Scroll View Content Position
Hi,
I have a scrollview which displays content vertically. Is there a property which lets me determine the y position of content (in the same way as I might set its height with contentHeight)?
Cheers
Mark
4 Answers
-
contentOffset does not help ?
take care it's an object so you'll need
the_scroll.contentOffset.x the_scroll.contentOffset.y
-
I tried to use this proporties, but it comes an exception:
scrollView.contentOffset is not an object
Here my code:
var win = Titanium.UI.currentWindow; var scrollView = Titanium.UI.createScrollView({ contentWidth:'auto', contentHeight:'auto', top:0, bottom:50, maxZoomScale:30, minZoomScale:1 }); var view = Ti.UI.createView({ backgroundImage:'../images/swasi.jpg', width:'100%', height:500, }); view.addEventListener('click',function(event){ var W = 1809; // Dimension of image var H = 2187; // .. var x = W* (event.x + scrollView.contentOffset.x)/scrollView.width/scrollView.scale; var y = H* (event.y + scrollView.contentOffset.y)/scrollView.height/scrollView.scale; label.text= 'x='+parseInt(x) + "\ny=" +parseInt(y); }); var label = Ti.UI.createLabel({height:50,bottom:0,text:'--'}); win.add(label); scrollView.add(view); win.add(scrollView); win.open();
In the demo I'am using this proporty in the callback. I dont know why it should be an error.
Best regards
Rainer -
For me, contentOffset is undefined. The top and rect.y properties for both the scrollview and it's child objects are not going to help us here either. The solution is to use the scroll event like so:
var scrollview = Ti.UI.createScrollView(); function scrollview_scroll_handler(e) { // e.y contains the current top y position of the contents of the scrollview Ti.API.info('Scrollview contents y offset: '+e.y); } scrollview.addEventListener('scroll', scrollview_scroll_handler);
Hope this helps someone :)
-
I could not get the contentOffset property or the getContentOffset method to work. I ended up creating a variable in which to store the Y value during the scroll event. When the scroll event stops the final value of Y is stored in the variable and accessible when needed. Hope this helps.