【问题标题】:How can I create a part of a filled circle using Core Graphics/Quartz如何使用 Core Graphics/Quartz 创建实心圆的一部分
【发布时间】:2012-05-24 20:06:40
【问题描述】:

我已经创建了 UIView 的子类,我正在尝试在我的 drawRect 方法中绘制圆的一部分。

我曾尝试使用bezierPathWithArcCenter 并填充它,但这只会产生一个饼形(图 3),而这不是我想要的。 我想画出你在图 1 和图 2 中看到的东西

也许我可以以某种方式剪辑一个完整的圆圈?圆圈周围的区域需要是透明的。

【问题讨论】:

    标签: iphone ios objective-c core-graphics quartz-graphics


    【解决方案1】:

    尝试用这个覆盖drawRect:

    - (void)drawRect:(CGRect)rect
    {
        [super drawRect:rect];
        CGContextRef context = UIGraphicsGetCurrentContext();
        float radius = 50.0f;
        float x_left = rect.origin.x;
        float x_left_center = x_left + radius;
        float y_top = rect.origin.y;
        float y_top_center = y_top + radius;
        /* Begin path */
        CGFloat white[4] = {0.0f, 204.0f/255.0f, 1.0f, 0.8f};
        CGContextSetFillColor(context, white);
        CGContextSetLineWidth(context, 1.0);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, x_left, y_top_center);
        CGContextAddArcToPoint(context, x_left, y_top, x_left_center, y_top, radius);
        CGContextAddLineToPoint(context,x_left, y_top + radius);
    
        CGContextFillPath(context);
    }
    

    它将绘制一个旋转的图像编号 2

    【讨论】:

      【解决方案2】:

      TompaLompas 的回答为我指明了正确的方向(使用弧线绘制部分)。然而完整的解决方案和答案是这样的:

      #define   DEGREES_TO_RADIANS(degrees)  ((M_PI * degrees)/ 180)
      - (void)drawRect:(CGRect)rect
      {
          [super drawRect:rect];
      
          CGContextRef ctx = UIGraphicsGetCurrentContext();
          int radius = self.frame.size.width / 2;
          CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
      //Image 2 
          CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
          CGContextAddArc(ctx, center.x, center.y, radius, DEGREES_TO_RADIANS(225), DEGREES_TO_RADIANS(315), NO);
          CGContextDrawPath(ctx, kCGPathFill);
      }
      

      【讨论】:

        猜你喜欢
        • 2011-02-19
        • 2010-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多