ios - 为什么给 addArcWithCenter 一个 0 度的 startAngle 会使它从 90 度开始?

标签 ios objective-c geometry core-animation uibezierpath

我正在创建一个 CAShapeLayer 用作 UIView 层的 mask 。我正在使用 UIBezierPath 来绘制形状层。它工作正常,除了我在绘制时得到一些奇怪的结果。几何体未按预期运行。我正在尝试在右上角绘制简单的“饼图切片”:

#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 documentation .

image

并且讨论了它是如何工作的(给定默认坐标系)

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 to YES draws the bottom half of the circle. However, specifying the same start and end angles but setting the clockwise parameter set to NO draws the top half of the circle.

这就是 addArcWithCenter:radius:startAngle:endAngle:clockwise: 角度的工作原理。如果这不是您所看到的,那么您计算的角度不正确。

关于ios - 为什么给 addArcWithCenter 一个 0 度的 startAngle 会使它从 90 度开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16192254/

相关文章:

objective-c - Core Text CTFrame 上的可点击区域

gps - 从经度,纬度转换为x,y

python - 如何使用 PIL 生成圆形缩略图?

ios - SK3DNode不旋转

ios - Swift 中的时间变量

iphone - 在 iPhone 中将图像、字符串文件添加到静态库

javascript - 将方形 div 拆分为 n 个较小的 div 算法

ios - Auth.auth.currentUser.uid 即使登录有效也无法正常工作

android - 使用 Kotlin 作为 Android 和 iOS 的 RestAPI 解析器

iphone - 为什么我的委托(delegate)方法没有被调用?