Ios 无法在CAShapeLayer上填充UIBezier

Ios 无法在CAShapeLayer上填充UIBezier,ios,objective-c,uibezierpath,cashapelayer,Ios,Objective C,Uibezierpath,Cashapelayer,我想在自定义视图中添加几个点,创建一条路径,关闭它并用颜色填充。在for循环中,我水平放置数千个点并用线连接它们。但是,我无法绘制闭合贝塞尔路径。有什么问题 UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0, screenHeight/2)]; [path addLineToPoint:CGPointMake(0, screenHeight/2)]; [path addLineToPo

我想在自定义视图中添加几个点,创建一条路径,关闭它并用颜色填充。在for循环中,我水平放置数千个点并用线连接它们。但是,我无法绘制闭合贝塞尔路径。有什么问题

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, screenHeight/2)];
 [path addLineToPoint:CGPointMake(0, screenHeight/2)];
[path addLineToPoint:CGPointMake(0, screenHeight/2)];

for (int i=0; i<=screenWidth; i+=screenWidth/stepNo) {
    [path addLineToPoint:CGPointMake(i, screenHeight/2)];
}
[path addLineToPoint:CGPointMake(screenWidth, screenHeight/2)];
[path addLineToPoint:CGPointMake(screenWidth, screenHeight)];
    [path addLineToPoint:CGPointMake(0, screenHeight)];

[path closePath];


//Create a CAShapeLayer that uses that UIBezierPath:
CAShapeLayer *shape = [CAShapeLayer layer];
shape.path = path.CGPath;
shape.fillColor = [UIColor colorWithRed:255/255.0 green:20/255.0 blue:147/255.0 alpha:1].CGColor;

// 5
[self.view.layer addSublayer:shape];

也许你可以试着在你的图层上设置背景色,以确保它与你期望的帧一起显示。我的子图层被显示是因为如果我使用构造而不是for循环[path addLineToPoint:CGPointMakescreenWidth,screenHeight/2];我可以看到矩形。然后你能试着这样构造你的路径吗:UIBezierPath*path=[UIBezierPath-bezierPath];[路径移动点:CGPointMake0,屏幕高度/2];[path addLineToPoint:CGPointMake0,屏幕高度];[path addLineToPoint:CGPointMakescreenWidth,screenHeight];[path addLineToPoint:CGPointMakescreenWidth,screenHeight/2];[path closePath];我已经尝试过这种结构,所有的东西都显示正确。然而,我有超过4个点,这就是为什么我需要循环。stepNo是如何定义的?