我正在创建一个 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 toYESdraws the bottom half of the circle. However, specifying the same start and end angles but setting theclockwiseparameter set toNOdraws 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模板网!
如何在不创建新位图的情况下拥有圆形、中心裁How to have a circular, center-cropped imageView, without creating a new bitmap?(如何在不创建新位图的情况下拥有圆形、中心裁剪的
如何使用 Quartz Core 绘制星星?How to draw stars using Quartz Core?(如何使用 Quartz Core 绘制星星?)
Android Catch Notes 应用程序,如圆形菜单Android Catch Notes App Like Circle Menu(Android Catch Notes 应用程序,如圆形菜单)
不存在这样的 acos 函数No such acos function exists(不存在这样的 acos 函数)
在与中心点成给定角度的直线相交的 UIView 矩形上Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point(在与中心点成给定角度的直线相交
在Android的地图视图上绘制一定半径的圆Draw circle of certain radius on map view in Android(在Android的地图视图上绘制一定半径的圆)