views:

516

answers:

1

Hi, I'm quite new to Cocoa, but would like to know how to change a CALayer's coordinate system. I read the article Layer Geometry and Transforms from the Core Animation Programming Guide, but still need some practical advice.

The default coordinate system has (0,0) at the lower left corner. I would like to change this to the center of the layer. Is that possible?

What I tried: using a CATransform3DTranslate as sublayerTransform to MidX and MidY of the layer's frame, the sublayers are displayed at the right place. At the setFrameSize event the translation should then be recomputed and applied again. I would rather like an automatic refresh (without 'steps').

Or should I use kCAConstraintMidX to relate the sub layer MidX to the layer MidX and use an offset? In fact, I am using CATextLayers as sublayers, and want to align the text centered, thus position the center of the text relative to the center of the super layer. I tried this, but it doesn't seem to refresh automatically when I resize the super layer.

+1  A: 

Check out Apple sample code called oalTouch which does this.

// Convert the view point to our layer / sound stage coordinate system, which is centered at (0,0)
CGPoint pointInLayer = CGPointMake(pointInView.x - [self frame].size.width / 2., pointInView.y - [self frame].size.height / 2.);