绘制bezierPath

时间:2016-09-14 17:26:52

标签: ios objective-c uibezierpath

我正在修补BezierPath并注意到我似乎无法弄清楚的事情。这是代码 -

UIBezierPath *path = [UIBezierPath new];

CGSize screenDimensions = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

CGPoint firstPoint = CGPointMake(screenDimensions.width/2, screenDimensions.height/2);
CGPoint secondPoint = CGPointMake(screenDimensions.width/2 + 10, screenDimensions.height/2 + 10);
CGPoint thirdPoint = CGPointMake(screenDimensions.width/2 - 10, screenDimensions.height/2 + 10);

[path moveToPoint:firstPoint];
[path addLineToPoint:secondPoint];
[path addLineToPoint:thirdPoint];
[path addLineToPoint:firstPoint];

[path closePath];

CAShapeLayer *tempLayer = [CAShapeLayer new];
[tempLayer setPath:path.CGPath];

UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(screenDimensions.width/2 - 30, screenDimensions.height/2 - 30, 100, 100)];

//    UIView *tempView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

[tempView setBackgroundColor:[UIColor blueColor]];
tempView.layer.mask = tempLayer;
[self.view addSubview:tempView];

如果我运行上面的代码块,则屏幕上添加的UIView上没有任何内容。但如果我要评论当前的" tempView"并取消注释当前评论的分配,它将完美地在屏幕上绘制。任何人都可以在设置框架时指出我在做错了什么或者是其他什么?

2 个答案:

答案 0 :(得分:1)

UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point1 = CGPointMake(100, 100);
CGPoint point2 = CGPointMake(200, 200);
CGPoint point3 = CGPointMake(300, 300);

[path moveToPoint:point1];
[path addQuadCurveToPoint:point3 controlPoint:point2];

CAShapeLayer *shape = [CAShapeLayer layer];
shape.path = path.CGPath;
shape.lineWidth = 5;
shape.strokeColor = [UIColor blueColor].CGColor;
shape.fillColor = [UIColor clearColor].CGColor;
shape.frame = self.view.bounds;
[self.view.layer addSublayer:shape];

您可以看到以下输出:

enter image description here

答案 1 :(得分:0)

这是一个简单的问题。

假设屏幕尺寸为320x480(和旧款iPhone)。这意味着firstPoint将是160, 240

在当前代码中,您的视图的框架为130, 210, 100, 100。它的宽度和高度均为100。

形状图层将相对于视图的边界绘制,而不是其框架。因此,就层和贝塞尔曲线路径而言,视图的边界为0, 0, 100, 100。由于firstPoint超出了这些范围,因此不会出现。它实际上是在视图的可见范围之外绘制的。

切换创建视图的两条线时,视图的框架变为0, 0, 320, 480。这意味着它的界限也是0, 0, 320, 480。现在视图要大得多,层和bezier路径适合并且可以看到。

正确的解决方案是根据将应用的视图大小创建贝塞尔路径的坐标,而不是屏幕的大小。这样,bezier路径将适合其视图,无论它有多大。

更像这样:

CGSize screenDimensions = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(screenDimensions.width/2 - 30, screenDimensions.height/2 - 30, 100, 100)];

//    UIView *tempView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

UIBezierPath *path = [UIBezierPath new];

CGPoint firstPoint = CGPointMake(tempView.bounds.size.width/2, tempView.bounds.size.height/2);
CGPoint secondPoint = CGPointMake(tempView.bounds.size.width/2 + 10, tempView.bounds.size.height/2 + 10);
CGPoint thirdPoint = CGPointMake(tempView.bounds.size.width/2 - 10, tempView.bounds.size.height/2 + 10);

[path moveToPoint:firstPoint];
[path addLineToPoint:secondPoint];
[path addLineToPoint:thirdPoint];
[path addLineToPoint:firstPoint];

[path closePath];

CAShapeLayer *tempLayer = [CAShapeLayer new];
[tempLayer setPath:path.CGPath];

[tempView setBackgroundColor:[UIColor blueColor]];
tempView.layer.mask = tempLayer;
[self.view addSubview:tempView];