问题描述
我正在创建一个 CAShapeLayer
以用作 UIView
层的掩码.我正在使用 UIBezierPath
来绘制形状图层.它工作得很好,除了我在画画时得到了一些奇怪的结果.几何图形未按预期运行.我正在尝试在右上角绘制简单的饼图":
I'm creating a CAShapeLayer
to use as a mask for a UIView
's layer. I'm using a UIBezierPath
to draw the shape layer. It's working fine, except I'm getting some odd results when I draw. The geometry is not behaving as expected. I'm trying to draw simple "pie slice" in the upper right corner:
#define degreesToRadians(x) ((x) * M_PI / 180.0)
...
// "layer" refer's to the UIView's root layer
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame =
layer.presentationLayer ?
((CAGradientLayer *)layer.presentationLayer).bounds : layer.bounds;
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.needsDisplayOnBoundsChange = YES;
CGFloat maskLayerWidth = maskLayer.bounds.size.width;
CGFloat maskLayerHeight = maskLayer.bounds.size.height;
CGPoint maskLayerCenter =
CGPointMake(maskLayerWidth/2,maskLayerHeight/2);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:maskLayerCenter];
[path addArcWithCenter:maskLayerCenter radius:(maskLayerWidth/2)
startAngle:degreesToRadians(0) endAngle:degreesToRadians(90) clockwise:YES];
[path closePath];
maskLayer.path = path.CGPath;
layer.mask = maskLayer;
最终的结果是在右下角绘制了一个饼图.圆弧的第一个点在 90 度处绘制,然后下降到 180 度.为什么即使我使用的是 0 度角和 90 度角,它也会这样做?
The end result is that a pie slice is drawn in the lower right corner. The first point of the arc is drawn at the 90 degrees, then down to 180. Why does it do this even though I'm using 0 and 90 degree angles?
推荐答案
此图片直接来自 文档.
还有一个关于它是如何工作的讨论(给定默认坐标系)
And there is a discussion of how it works (given the default coordinate system)
讨论
此方法添加从当前点开始的指定弧.创建的圆弧位于指定圆的周长上.在默认坐标系中绘制时,起点和终点角度以图1中所示的单位圆为基准.例如,指定起始角为 0 弧度,结束角为 π 弧度,并将顺时针参数设置为 YES
绘制圆的下半部分.但是,指定相同的开始和结束角度但将 顺时针
参数设置为 NO
会绘制圆的上半部分.
Discussion
This method adds the specified arc beginning at the current point. The created arc lies on the perimeter of the specified circle. When drawn in the default coordinate system, the start and end angles are based on the unit circle shown in Figure 1. For example, specifying a start angle of 0 radians, an end angle of π radians, and setting the clockwise parameter toYES
draws the bottom half of the circle. However, specifying the same start and end angles but setting theclockwise
parameter set toNO
draws the top half of the circle.
这就是 addArcWithCenter:radius:startAngle:endAngle:顺时针:
的角度工作原理.如果这不是您所看到的,那么您计算的角度不正确.
This is how the angles work for addArcWithCenter:radius:startAngle:endAngle:clockwise:
. If that isn't what you are seeing then you are calculating your angles incorrectly.
这篇关于为什么给 addArcWithCenter 一个 0 度的 startAngle 使它从 90 度开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!