The y coordinate is annoying, because 0 is the bottom on the mac and the top on iOS. But I don’t want to flip everything . . . images, for example, are the same way up on both platforms.
What is the most efficient way to have everything work on both platforms?
The way I’ve approached this, and the way that is used in the Core Plot framework, is to base all of my layer coordinates on the Quartz coordinate system (where the origin is at the bottom left). On iOS, I then create a custom layer-hosting view that un-inverts the coordinate system of its hosted layer. CALayers still have the same coordinate system on iOS, it’s just that CALayers that back UIViews have their coordinate system inverted.
To do this, I use code like the following in the initialization of the UIView:
and then I create one main hosted layer as a sublayer of that UIView’s backing layer, and add all of my custom layers to that hosted layer.
Again, you can look at the code for the Core Plot framework to see how this works in practice, because that’s how this framework handles layer-based drawing across the two platforms with almost all of the same code.
There are just a few conditions you have be careful of on iOS, such as the NSString drawing methods which need to be flipped to account for the Quartz coordinate space.