为什么给addArcWithCenter一个0度的startAngle使它开始于90度? [英] Why does giving addArcWithCenter a startAngle of 0 degrees make it start at 90 degrees?

查看:842
本文介绍了为什么给addArcWithCenter一个0度的startAngle使它开始于90度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个 CAShapeLayer 作为 UIView 的图层的掩码。我使用 UIBezierPath 来绘制形状图层。它运行良好,除非我在绘制时遇到一些奇怪的结果。几何不符合预期。我试图在右上角画一个简单的pie slice:

  #define degreesToRadians(x)((x )* M_PI / 180.0)

...

//layer指向UIView的根层

CAShapeLayer * maskLayer = [CAShapeLayer层];

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)顺时针:YES];

[path closePath];


maskLayer.path = path.CGPath;

layer.mask = maskLayer;

最终的结果是在右下角绘制一个饼图片。为什么它会这样做,即使我使用0度和90度角?

解决方案

该图片直接来自文档 b



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


讨论
此方法添加指定的弧开始于当前的一点。创建的弧位于指定圆的周边。在默认坐标系中绘制时,起始角和结束角基于图1 中显示的单位圆。例如,指定0弧度的起始角度,π弧度的结束角度,并将顺时针参数设置为 YES 会绘制圆的下半部分。但是,指定相同的开始和结束角度,但将顺时针参数设置为 NO 会绘制圆的上半部分。

这是角度如何适用于 addArcWithCenter:radius:startAngle:endAngle:顺时针:。如果这不是你所看到的,那么你正在计算你的角度不正确。


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;

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?

解决方案

This image comes straight from the documentation.

And there is a discussion of how it works (given the default coordinate system)

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.

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度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆