Is it possible to anchor a CGPattern-image background color to the center of a CALayer?
-
11-04-2021 - |
Pregunta
I've set up a layer as follows:
CALayer *layer = [CALayer layer];
layer = /* create a CGColorRef with a pattern image */;
This layer is simply displayed in my window. My draw/release functions for the pattern are as follows:
void drawPattern(void *info, CGContextRef c) {
CGContextDrawImage(c, CGRectMake(0, 0, CGImageGetWidth(info), CGImageGetHeight(info)), info);
}
void releaseInfo(void *info) {
CGImageRelease((CGImageRef)info);
}
By default, the pattern is anchored at the lower-left corner of the window, so when I resize the window, that point is fixed.
I figured out that I can anchor it at the upper-left corner with two steps:
- set
layer.geometryFlipped = YES;
- use
CGAffineTransformMakeScale(1, -1)
as an argument toCGPatternCreate
instead ofCGAffineTransformIdentity
.
But how can I anchor it at the center? (Changing the layer's anchorPoint
doesn't seem to do anything.)
Solución
I think you'll have to pass the layer, not the image, as the info
parameter, and have your drawing function retrieve the layer's frame
and use that to compute a translation to apply.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow