0
votes

I'm developing some kind of card game in which my view objects (cards, etc) are all CALayers. These objects are added as sublayers of a mainBoard (a CALayer), which, in turn, is a sublayer of the [UIView layer] property of my mainView. Everything is Ok, I can do hit testing to verify which layer is being touched through the touchesBegan:withEvent: callback of the mainView class normally, and so on.

However, I need to have a scroll inside my mainBoard to show "bigger" CALayers, so I tried first adding the "bigger" CALayer inside a CAScrollLayer, but I noticed that the CAScrollLayer doesn't really scroll (doesn't handle user input, neither draws scrollbars). So, the workaround would be to add an UIScrollView directly to the mainView UIView. The UIScrollView scrolls perfectly, until I try to add the "bigger" layer to it, by using [scrollView.layer addSublayer:<bigger layer>].

Does anyone has any idea of how I can have "scrollable" objects inside a CALayer?

Thanks!

1
what exactly happens when adding big layers to the scrollview?, Besides I think adding sublayer directly to scrollView.layer is not a good idea, why don't you add them to a layer of a view contained by your scrollview?nacho4d
Can you define what the "bigger" CALayers are? Can you briefly describe why the "bigger" items must be CALayers? By reading your question, I wonder why you are not keeping it simple and just using UIScrollView - it has everything you've asked for, which would be better to not re-implement.Mark
My whole application consists of CALayers objects because I'm working in Core Animation level instead of UIKit abstraction level. The problem is that the Core Animation doesn't provide a Scrollable component, so I had to do a workaround mixing UIKit objects (UIScrollView) and CoreAnimation objects (CALayers) and making them working seamlessly. This is not a problem since UIKit is just a wrapper for CoreAnimation. ThanksEduardo Coelho

1 Answers

0
votes

nacho4d pointed the answer for my question. Replying to mark: the "bigger" layer (a layer whose bounds is bigger than the area available in the screen (and so, needs to be contained in a scrollable area)).

The solution was to first wrap the "bigger" layer in a UIView:

UIView *wrapperView = ...
[wrapperView.layer addSublayer:<bigger_layer>];

afterwards, I can add the view as a subview of the UIScrollview:

[<uiscrollview> addSubview:wrapperView];

// set up scrolling properties
<uiscrollview>.contentSize = <bigger_layer>.frame.size;

The problem was that I was trying to add the "bigger" layer as a sublayer of the UIScrollview directly, instead, wrapping it in a UIView and adding it as a subview worked.

Thanks