4
votes

I am currently using the CCScrollLayer (in the cocos2d extension classes) class to implement a menu system. It's working great but I would like to have other buttons on the screen and the scrollable area is the entire screen by default.

I tried messing with content size but no dice. After doing some reading, I found that the content size is set to the screen size per CCLayers behavior. A user suggested wrapping it in a CCNode and scaling, but this did not help.

Any suggestions or sample code? I'd have to think this should be possible.

3

3 Answers

0
votes

CAScrollLayer is surprisingly simple, which may be confusing.

Just add the content layer to it:

[_scrollLayer addSublayer:_contentLayer];

To set the rect you want to be visible on the screen set bounds or frame of the scroll layer:

[_scrollLayer setBounds:visibleBounds];

Set the content's size respectively. The content's size can be bigger or smaller, it does not matter.

[_contentLayer setBounds:currentContextBounds];

If the content is bigger and you want to scroll to a certain point use scrollToPoint: or scrollToRect: methods of the scroll layer.

You need to implement your own scrolling indicators, bars, etc. if desired.

0
votes

Although I am using table view and not the scroll view, both share the same parent [scroll view doh]. Have you turned on clipToBounds after setting content size?

I would recommend using this constructor:

/**
 * Init this object with a given size to clip its content.
 *
 * @param size view size
 * @return initialized scroll view object
 */
- (id)initWithViewSize:(CGSize)size;
0
votes

Thanks everyone for the help, unfortunately it seems you cannot change the size of a CCLayer (or the contentSize has no effect on touch events) without basically rewriting a lot of code.

However, I found a workaround that seems to be working. In the ccTouch events began and moved, I check my desired bounds after converting my UITouch to cocos2D space. If they are not in the my bounds I return, and in the case of move I manually call TouchEnded. TouchEnded also checks the initial touched point, and if it is was out of bounds it ignores it.

Things seems to be working as desired. I will post more information if I find it. Thanks again everyone.