我有一个 CAShapeLayer,用于在条形图中绘制小点。问题是它们在视网膜显示器上都太大了。我已经设置了 contentsScale 并检查了它是否正在设置。我仍然认为有它或没有它没有区别!
CAShapeLayer *lineShapeLayer = [CAShapeLayer layer];
lineShapeLayer.contentsScale = [[UIScreen mainScreen] scale];
CGMutablePathRef path = CGPathCreateMutable();
lineShapeLayer.backgroundColor = [UIColor clearColor].CGColor;
lineShapeLayer.bounds = CGRectMake(0, 0, self.backgroundLayer.bounds.size.width, 1.5);
lineShapeLayer.anchorPoint = CGPointMake(0, 0);
lineShapeLayer.position = CGPointMake(0, rint((self.verticalPartitionHeight)*i));
lineShapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1], [NSNumber numberWithInt:2], nil];
lineShapeLayer.strokeColor = [UIColor blackColor].CGColor;
lineShapeLayer.lineWidth = 1;
NSLog(@"contentsScale: %f", lineShapeLayer.contentsScale);
CGPathMoveToPoint(path, nil, 0, .5);
CGPathAddLineToPoint(path, nil, lineShapeLayer.frame.size.width, .5);
[lineShapeLayer setPath:path];
[self.backgroundLayer addSublayer:lineShapeLayer];
如果我用 PNG 制作它,这是视图:
这是带有 CAShapeLayer 代码的视图(设置为线宽 0.5 甚至。):