small medium large xlarge

30 Mar 2009, 02:22
eduardito (4 posts)

Hello Bill,

your book is very good, it helped a lot. Unfortunately I’m having big trouble doing something with my iPhone app.

Basically I have a sub class of a UIScrollView.. in the initialization method I intialize a CASrollLayer and add that (after doing some config stuff) as a sub layer to self.layer

The program works good and all, the problem is that I would like to insert a lot of layers in the scroll, so that while I drag I can keep looking at them.. I managed that.. only with images tho. I usually allocate and initialize UIImageViews and add their layers to the CAScrollLayer as sublayers

My problem is that I would like to add (this same thing) but I want to them to be tapable and be able for my program to know which layer was tapped inside the scroll…

I was thinking in subclassing CALayer and giving some instance variables to know a little bit about the particular layer, but they don’t have any event handling since they don’t inherit from UIControl..

What can I do?

30 Mar 2009, 20:01
Bill Dudney (917 posts)


Thanks for the kind words!

Generally speaking its bad news to mess with layers that are under the direct control of a view (such as a UIImageView’s layer as you mention above). Instead get the CGImage from the UIImage and make that the contents of your layer.

To handle events use the hitTest: method to find the layer that was hit. Typically you’d call this from the touchesEnded:withEvent: method. Once you find the layer you can use custom properties from your subclass or whatever other scheme you come up with to decide what to do with the newly selected layer.

Good luck!

31 Mar 2009, 04:13
eduardito (4 posts)

Thank you Bill,

Bill I was thinking in just having a UIScrollView and inside of it create CAScrollLayer (in the init code) and add as a sublayer so that I can have the methods for events available (the ones for a scrollview)

all layers (from the imageviews) are added as sublayers of the scroll layer, so I will always have an integer to locate any given layer in the sublayers array. And this integer always changes because whenever you scroll.. the integer selects a given layer. I would just use the events for the scroll and do actions to a layer based on this integer. Instead of doing hittest etc etc I guess I could then simply create directly layers and add CGImages to it instead of allocating UIImageViews!

This might be out of topic but, if I add say 100 sublayers and while I scroll and would like to load 100 more. How do you recommend I do this? any suggestions Bill?

31 Mar 2009, 13:54
Bill Dudney (917 posts)

Why not add the UIImageView’s as subviews of the UIScrollView?

You really should not be manipulating the layer’s that belong to a view. I.e. you should never grab the layer that belongs to a UIScrollView and add sublayers to it. That will mess up the connection between the view and its layer and potentially cause strange unexpected behavior.

Instead just use view’s behind the scenes things will still be using layers anyway and you will get event handling.

Good luck!

You must be logged in to comment