問題描述
我正在創建一個 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 度開始?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!