In this tutorial, you'll learn what a CALayer is, and see 10 examples of using CALayers for cool effects, like shapes, gradients, and even particle systems.
Thank you for this. FYI. In going through the CATileLayer tutorial I got a backtrace:
LayerPlayer[1522:262128] [reports] Main Thread Checker: UI API called on a background thread: -[UIView bounds]
It appears in TilingView.swift, you are accessing bounds from draw(). XCode 9 reports “UIView.bounds must be used from main thread only” on line 59.
I have the same issue. the problem is that it crash there randomly, when you are using the app compiled for production.
so it is more bad than a simple warning…
Would anyone know if CATextLayer inherit the transform of the parent layer? I am trying it out, and have set the root layer transform to be flipped, CGAffineTransform(a: 1.0, b: 0, c: 0, d: -1.0, tx: 0, ty: 0).
But when I add a CATextLayer to it, layer.addSublayer(labelLayer), it is rendered incorrectly, ignoring the transform that is set for the parent. The other layers, CAShapeLayers, do inherit the transform and therefore are rendered correctly.
CATextLayer should inherit the transform from the parent layer.
Here’s a snippet I used to do just that - it did apply transform to CATextLayer sublayer.
Everything is drawn correctly according to the view hierarchy you’ve set up
There are two problems:
The width of the label is too big it seems, the text is centered so the text seems to be far away from the point it should describe. Try to decrease width from 100 to (smaller value) or use kCAAlignmentLeft.
The CATextLayer gets flipped as expected, but that means it’s unreadable. You have to flip it back locally to make it readable.
The second one is tricky. You basically hit a math problem. Please read this and use a piece of paper and try to draw everything out.
You need to put CATextLayer in a wrapper view:
wrapperView.frame = CGRect(x: point.x, y: point.y, width: 100, height: 20)
textLayer.frame = CGRect(x: 0, y: 0, width: wrapperView.width, height: wrapperView.height)
wrapperView.addSublayer(textLayer)
layer.addSublayer(wrapperView)
textLayer.setAffineTransform(flippedTransform) //Flip back the text but without moving it in space
If what you are saying is correct, does that mean that the CATextLayer ignores the parent layer transform when drawing the text, not necessarily the layer, but the text string that is drawn in it?
Also, I picked 100 as width somewhat arbitrarily, the behavior is the same with a width of 20. The width and alignment are not the main problem I am trying to solve.
CATextLayer should inherit the transform from the parent layer.
You’re right, my mistake. That’s actually incorrect, sorry. What I meant and what is happening is that the CATextLayer is affected by the transform, but does not “inherit” the value. CATextLayer transform is identity, it does not inherit the value from the parent. It’s true for every layer. In fact that’s the standard in any 2d/3d drawing framework - children do not “inherit” transforms, their transforms are identity by default.
Imagine the transforms being applied from back to front. That means that everything is drawn normally including text and in the end the whole view gets flipped.