ios – 如何在UIView中绘制不同颜色的多个UIBezierPath

前端之家收集整理的这篇文章主要介绍了ios – 如何在UIView中绘制不同颜色的多个UIBezierPath前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在uiview中绘制多个UIBezierPath,具有不同的笔画和填充颜色.

这是代码

  1. - (void)drawRect:(CGRect)rect {
  2.  
  3. context = UIGraphicsGetCurrentContext();
  4.  
  5. [[UIColor grayColor] setFill];
  6. [[UIColor greenColor] setStroke];
  7.  
  8. UIBezierPath *aPath = [[UIBezierPath alloc] init];
  9. [aPath moveToPoint:CGPointMake(227,34.25)];
  10. [aPath addLineToPoint:CGPointMake(298.25,34.75)];
  11. [aPath addLineToPoint:CGPointMake(298.5,82.5)];
  12. [aPath addLineToPoint:CGPointMake(251,83)];
  13. [aPath addLineToPoint:CGPointMake(251,67.5)];
  14. [aPath addLineToPoint:CGPointMake(227.25,66.75)];
  15. [aPath closePath];
  16. aPath.lineWidth = 2;
  17. [aPath fill];
  18. [aPath stroke];
  19.  
  20.  
  21. UIBezierPath* aPath2 = [[UIBezierPath alloc] init];
  22. [aPath2 moveToPoint:CGPointMake(251.25,90.5)];
  23. [aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
  24. [aPath2 addLineToPoint:CGPointMake(298.5,83)];
  25. [aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
  26. [aPath2 closePath];
  27. aPath2.lineWidth = 2;
  28. [aPath2 fill];
  29. [aPath2 stroke];
  30. [paths addObject:aPath2];

问题是笔画和填充颜色是在当前上下文中设置的.
在同一个CGContextRef中可以用不同的颜色绘制不同的UIBezierPath吗?

或者我必须在单独的uiview中绘制每个UIBezierPath?

解决方法

你应该添加
  1. [desiredStrokeColor setStroke];
  2. [desiredFillColor setFill];

表明这些是在这种情况下必须进一步使用的新颜色.你每次打电话之前都应该这样做

  1. [aNewPath fill];
  2. [aNewPath stroke];

以便用这些颜色绘制路径.

不需要为每个贝塞尔路径使用新的视图.

猜你在找的iOS相关文章