UIBezierpath填充自定义形状

时间:2014-04-10 01:28:28

标签: ios objective-c uiview drawing uibezierpath

我试图填补这个形状。但是使用我的代码,它会填满整个视图,而不是内部形状。

https://www.dropbox.com/s/nplglh39xyvn1cr/%EC%8A%A4%ED%81%AC%EB%A6%B0%EC%83%B7%202014-04-10%2010.21.04.png

UIBezierPath *shape = [UIBezierPath bezierPathWithRect:self.bounds];

[shape moveToPoint:CGPointMake(self.bounds.size.width*0.15, self.bounds.size.height*0.6)];

[shape addCurveToPoint:CGPointMake(self.bounds.size.width*0.85, self.bounds.size.height*0.4)
         controlPoint1:CGPointMake(self.bounds.size.width*0.1, self.bounds.size.height*0.3)
         controlPoint2:CGPointMake(self.bounds.size.width*0.6, self.bounds.size.height*0.5)];

[shape addCurveToPoint:CGPointMake(self.bounds.size.width*0.15, self.bounds.size.height*0.6)
         controlPoint1:CGPointMake(self.bounds.size.width*0.9, self.bounds.size.height*0.7)
         controlPoint2:CGPointMake(self.bounds.size.width*0.4, self.bounds.size.height*0.5)];
[shape closePath];
[[UIColor blueColor] setFill];
[shape fill];
[[UIColor brownColor] setStroke];
[shape stroke];

有什么问题?当我绘制三角形或任何其他形状时,它的效果非常好。

2 个答案:

答案 0 :(得分:0)

UIBezierPath *shape = [UIBezierPath bezierPathWithRect:self.bounds];

这就是问题所在。您正在创建具有视图边界的路径。请改用UIBezierPath *shape = [[UIBezierPath alloc] init];

答案 1 :(得分:0)

试试这个:

我认为你只会得到这种形状......

UIBezierPath *shape = [[UIBezierPath alloc] init];

[shape moveToPoint:CGPointMake(45,78)];

[shape addCurveToPoint:CGPointMake(67,51)
         controlPoint1:CGPointMake(43,78)
         controlPoint2:CGPointMake(55,54)];

[shape addCurveToPoint:CGPointMake(144,45)
         controlPoint1:CGPointMake(80,49)
         controlPoint2:CGPointMake(137,60)];

[shape addCurveToPoint:CGPointMake(143,77)
         controlPoint1:CGPointMake(157,34)
         controlPoint2:CGPointMake(143,77)];

[shape addCurveToPoint:CGPointMake(43,84)
         controlPoint1:CGPointMake(143,77)
         controlPoint2:CGPointMake(43,65)];

[shape addCurveToPoint:CGPointMake(43,78)
         controlPoint1:CGPointMake(43,103)
         controlPoint2:CGPointMake(43,78)];
[shape closePath];
[[UIColor blueColor] setFill];
[shape fill];
[[UIColor brownColor] setStroke];
shape.lineWidth = 1;
[shape stroke];

改变CGPoint取决于你的形状......


如果还有更多的困惑,只需尝试使用此工具并通过绘制形状来获取代码......

工具名称:PaintCode(http://www.paintcodeapp.com/

下载链接http://www.paintcodeapp.com/wp-content/uploads/2013/10/paintcode-trial-1-3-3.zip