Hey, i’m using CCScrollView to show a list of nodes. Here is the initialization:
@ ListLayer* list = ListLayer::create();
list~~>setPosition;
list~~>setContentSize(CCSizeMake(250, 900));@
The scrolling works, but the thing is that when i scroll upwards (going down in the list) the list bounces back to the original position. If i scroll downwards it does not bounce back and it leaves a blank space at the top. The behavior is exactly the opposite than the one i want. How can i fix this?
BTW; i’m using the last version of CCScrollView (https://github.com/cocos2d/cocos2d-x/blob/gles20/extensions/GUI/CCScrollView/CCScrollView.cpp)
Thanks!
Unfortunately your solution did not work for me. I actually got it to scroll in the direction I wanted to once, but I didn’t understand why. I think the reason why it is scrolling upside down is due to the fact that the positive direction for Y is downwards. So I tried to set the position of the layer so that it would be on the negative side of Y. I think that is how I got it to work once. I will continue to experiment with this idea.
You can try initializing the scrollview with a size, in the case of CCScrollView it is not the same as setting the content size afterwards. Something like CCScrollView::create(CCSizeMake(layer->getContentSize().width,winSize.height), NULL);
The answer lies in CCScrollView::minContainerOffset@ and CCScrollView::maxContainerOffsetCCScrollViewworks in *OpenGL coordinates* (as opposed *window coordinates*) - values are relative to (left, bottom) with positive Y axis going upwards. Also, keep in mind the scroll view’s positioning and thecontainer are anchored (CCNode::setAnchorPoint) to (left, bottom). When you scroll down (move/pull the content upwards to see content below the cut/clip), you see the content below the bottom edge of the screen but it bounces back the moment you let go of the touch/drag because maxContainerOffset` returns (0, 0) and you’ve just tried to move to a positive content offset.
The (attached) diagram shows the state of the scrollview and the container when they’re created/initialized. This is the state or the coordinates to”think in" when setting up and positioning the child elements and container. The gray rectangle (left, bottom) shows the valid region for scrolling the container. Imagine the container’s anchor point moves inside it.
To see the container scrolled to the top to begin with (what you’d expect when working in window coordinates), set the content offset accordingly (immediately after setting it up). This will give the expected results/behavior.
scrollView~~>setContentOffset), false);
A more complete example is in the edited code below.
<pre>
CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
CCLayer* scrollContainer = CCLayer::create; // Container for the scroll view
scrollContainer~~>setAnchorPoint; // CCScrollView does this too when it’s set as the container.
// Content for the container
CCSprite tallContentA = CCSprite::create;
tallContentA >setPosition);
CCSprite *tallContentB = CCSprite::create;
tallContentB>setPosition);
scrollContainer~~>addChild;
scrollContainer~~>addChild;
float scrollContainerHeight = tallContentA~~>getContentSize.height + tallContentB~~>getContentSize.height;
scrollContainer~~>setPosition;
scrollContainer~~>setContentSize);
// Set up scroll view
CCScrollView scrollView = CCScrollView::create;
scrollView~~>setPosition(CCPointZero);
scrollView~~>setDirection;
// ScrollView initializes at the . The container also gets positioned relative to that and goes Y-up.
// Pre-set it to the value CCScrollView::minContainerOffset will return when it’s scrolled to the top
//
scrollView~~>setContentOffset(ccp(0.f, (winSize.height-scrollContainerHeight*1.05f)), false);
I still have problems in understanding and would like to clarify. Would you please advice?
I am using cocos2d-x-2.2, including its CCScrollView.
The entire scrollview content should layout like this:
And I expect when first initialized, the green sprite will be shown full screen (my view is 960x1280) and I’ll be able to scroll my moving up to see the pink sprite on the bottom. However, it doesn’t work. My initial view shows the bottom part of the view, and when I scroll up or scroll down, everything disappear (move swiftly to above).
m_pLayerScrollContainer~~>setPosition;
right after
FLOAT fContentHeight = m_pLayerScrollContainer~~>getContentSize().height;
But perhaps that’s just missing in what you’ve posted on the forum and exists in the complete code.
More importantly, the example code you’ve posted seems to be missing the part which will “scroll down” the container to the position one would expect in window coordinate based UIs (see code lines 21 to 24 in my post above which explain this).
Add:
m_pViewScroll~~>setContentOffset), false);
You seem to have used setContentSizeinstead ofsetContentOffset. Remove:
m_pViewScroll~~>setContentSize(ccp(windowSize.width, windowSize.height-2496.0f));