【问题标题】:Drawing Hollow circle in iPhone在 iPhone 中绘制空心圆
【发布时间】:2011-05-02 11:54:50
【问题描述】:

我需要画下图

灰色部分是我想在另一个图像上绘制的部分是什么我需要使用 CGContext 方法使用的代码,我尝试使用 CGContextAddArc 但失败了,因为当我填充笔触时,中心空心也填充了灰色纹理.

任何帮助表示赞赏。

信息:我有完整的蓝色图像,我需要在蓝色图像上方添加半圆

谢谢

【问题讨论】:

  • 在尝试使用 cgcontext 创建甜甜圈耻辱然后将其添加到图层或 shapelayer 时,您将如何解决... 抱歉,但我不明白这一点在这一刻,所以在mo只是拍摄空白

标签: iphone cgcontext geometry


【解决方案1】:

查看 Core Graphics 文档中的 Filling a Path。基本上,您所做的是在路径中添加两条弧线(外部和内部),然后使用 Core Graphics 填充规则来发挥您的优势。代码看起来像这样:

CGMutablePathRef path = CGPathCreateMutable();

// Add the outer arc to the path (as if you wanted to fill the entire circle)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the inner arc to the path (later used to substract the inner area)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the path to the context
CGContextAddPath(context, path);

// Fill the path using the even-odd fill rule
CGContextEOFillPath(context);

CGPathRelease(path);

【讨论】:

    【解决方案2】:

    进一步研究Ole Begemann 在他的回答中提到的内容和一些修改,我能够达到要求。

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_grey_pattern.png"]].CGColor);
    CGMutablePathRef path = CGPathCreateMutable();
    CGContextSetLineWidth(context, 40);
    CGPathAddArc(path, NULL, aRect.size.width/2, aRect.size.height/2, 45, 0*3.142/180, angle*3.142/180, 0);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);
    CGPathRelease(path);
    

    所以我只使用了一条弧线而不是两条弧线,并以更高的宽度抚摸它。

    【讨论】:

      【解决方案3】:

      这是切线但有点相关。这是我的甜甜圈(或空心圆)的 Swift 绘图代码。

      class func drawDonut(donutColor: UIColor, rect: CGRect, innerDonutProportion: CGFloat) {
      
          //// Variable Declarations
          let innerDonutSide: CGFloat = rect.size.width * innerDonutProportion
          let innerDonutOffset: CGFloat = 0.5 * (rect.size.width - innerDonutSide)
          let innerDonutRect = CGRectMake(innerDonutOffset, innerDonutOffset, innerDonutSide, innerDonutSide)
      
          //// InnerCircle Drawing
          let innerCirclePath = UIBezierPath(ovalInRect: innerDonutRect)
      
          //// Outer Circle Drawing
          let outerCirclePath = UIBezierPath(ovalInRect: rect)
      
          // Clip out the innerCirclePath
          outerCirclePath.appendPath(innerCirclePath)
          outerCirclePath.addClip()
          outerCirclePath.usesEvenOddFillRule = true;
      
          // and Fill the outerCircle
          donutColor.setFill()
          outerCirclePath.fill()
      }
      

      【讨论】:

      • 不错!但是对于 rect.origin 不是 (0,0) 的一般情况,您应该改用: let innerDonutRect = CGRectMake(rect.origin.x + innerDonutOffset, rect.origin.y + innerDonutOffset, innerDonutSide, innerDonutSide)。除此之外,您的代码非常适合我。
      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 2019-01-21
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多