简体   繁体   English

CAShapeLayer strokeColor从图像图案UIColor到CGColor上下颠倒

[英]CAShapeLayer strokeColor from image pattern UIColor to CGColor upside down

I'm using CABasicAnimation to draw all UIBezierPaths i have in an Array using CAShapeLayer 我使用CABasicAnimation绘制所有UIBezierPaths我在使用数组CAShapeLayer

CAShapeLayer *shapeLayer = [CAShapeLayer layer];    
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = path.color.CGColor;

My path.color is a UIColor from image pattern. 我的path.color是图像图案的UIColor When it is converted to CGColor the image pattern is upside down. 将其转换为CGColorimage pattern会颠倒。 I know that is because iOS and core graphics use different starting points for drawing. 我知道这是因为iOS和核心图形使用不同的绘图起点。

I have tried 我努力了

shapeLayer.transform = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0);

and

shapeLayer.geometryFlipped = YES;

But none of them worked. 但是他们都不起作用。

How do I flip the strokeColor image pattern 180 degree to match with the origin UIColor ? 如何将strokeColor图像图案翻转180度以与原点UIColor匹配?

As you have correctly suspected, the coordinate system differs. 正如您已经正确怀疑的那样,坐标系有所不同。 In this case, the origin is at the bottom left at (0, 0) . 在这种情况下,原点位于(0, 0)的左下角。

So what you could do to compensate that is to either flip the image yourself using an image editor or do it in code like so: 因此,您可以采取的补偿措施是使用图像编辑器自己翻转图像或使用以下代码进行操作:

UIImage *flippedPatternImage = [UIImage imageWithCGImage:patternImage.CGImage scale:1.0f orientation:UIImageOrientationLeftMirrored];

UIColor *colorWithImagePattern = [UIColor colorWithPatternImage:flippedPatternImage];

And finally assign it to the strokeColor : 最后将其分配给strokeColor

shapeLayer.strokeColor = path.color.CGColor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM