ios - CAShapeLayer strokeColor 从图像模式 UIColor 到 CGColor 颠倒

标签 ios objective-c core-graphics calayer cashapelayer

我正在使用 CABasicAnimation 绘制我在数组中使用 CAShapeLayer 的所有 UIBezierPaths

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

我的 path.color 是来自图像模式的 UIColor。当它转换为 CGColor 时,图像模式 是颠倒的。我知道这是因为 iOS 和核心图形使用不同的绘图起点。

我试过了

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

shapeLayer.geometryFlipped = YES;

但它们都不起作用。

如何将 strokeColor 图像图案翻转 180 度以匹配原点 UIColor

最佳答案

正如您猜想的那样,坐标系不同。在这种情况下,原点位于左下角 (0, 0)

因此,您可以通过使用图像编辑器自己翻转图像或使用如下代码来弥补这一点:

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

UIColor *colorWithImagePattern = [UIColor colorWithPatternImage:flippedPatternImage];

最后将它分配给 strokeColor:

shapeLayer.strokeColor = path.color.CGColor;

关于ios - CAShapeLayer strokeColor 从图像模式 UIColor 到 CGColor 颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563031/

相关文章:

ios - 如何使用 UIBezierPath 绘制一个倾斜的椭圆

ios - 键值观察不适用于zoomScale

objective-c - 检测鼠标被按住

ios - 比较两个 CGPoints 是否相等 : returning not equal for two objects that output same point?

ios - 调整使用 CoreGraphics 绘制的图像的大小

ios - Hackintosh 10.12.6 上 Xcode 9 beta 4 的空白模拟器屏幕

ios - 当应用程序在 iOS 中运行时,是否会在关闭后台通知的情况下收到静默通知?

ios - XCode 不会在从 LLDB 调用的方法中的断点处停止

ios - 画一个圆圈,用不同的颜色填充不同的部分

ios - TableView 行中 Collection View 的同步水平滚动