ui:ScrollView setContentOff equivalent method

  • shangmike99
    Likes 1

    Problem Description

    While I was migrating a cocos2dx source code from V2 to V3, (VS2010 to VS2013)

    I realised CCSCrollView is obsolete and I couldn’t include cocos2d::extension::ScrollView at all with VS2013

    I was forced to use ui::ScrollView instead but one crucial method was missing, i..e setContentOffset ,

    which allowd me to scroll to an exact position after ScrollView loaded with data.

     

    Solution Description

    The closest equivalent method to setContentOffset i could find is scrollToPercentVertical / scrollToPercentHorizontal.

    However the method is quite low level that it only provides the scrolling feature leaving all the necessary calculations to us.

    To calculate the right scrolling position this will be the formulae to do so, for vertical position (found out after spending the whole damn day)

            float ContentHeight = 6520; // the long content list height
            float WinHeight     = 960;  //Screen Resolution Height

            float posYAdjusted = posY + WinHeight/2;
            if (posYAdjusted < WinHeight) posYAdjust = WinHeight; 
            if (posYAdjusted > ContentHeight-WinHeight/2) posYAdjusted = ContentHeight-WinHeight/2;
            float pyPercent = 100.0f - ((py-WinHeight) / (ContentHeight-WinHeight)*100.0f);
            layer->scrollToPercentVertical(pyPercent, 0.0, false);

    posY = position to scroll to,  posYAdjusted = adjust the position to mid of the screen

    WinHeight = The window height of the screen resolution e.g. 960, 1024, etc.

    ContentHeight = The height of long content list to be loaded, e.g. 7000, 9000, etc.

    pyPercent = position in % to scroll to 

    For horizontal scroll you will have to improvise it by yourself :)

    Hope this will help!

     


  • Sonar Systems admin
    Likes 0

    Fantastic


    This reply has been verified.

Login to reply