ios - 用quartz2d绘图时如何将矩形夹在圆周围?

标签 ios quartz-graphics

我由名为 Bubble 的 UIView 子类组成,我想用它来绘制一个实心圆。这是 drawRect: rect 方法

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0);
    // And draw with a blue fill color
    CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(ctx, 2.0);

    CGRect rrect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
    CGFloat radius = 30.0;

    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);

    // Start at 1
    CGContextMoveToPoint(ctx, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(ctx, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, radius);
    // Close the path
    CGContextClosePath(ctx);
    // Fill & stroke the path
    CGContextDrawPath(ctx, kCGPathFillStroke);
}

我从苹果示例 Quartzdemo 中获取了代码。

我的问题是我在矩形内画圆,但我仍然看到所画矩形的角。

如何剪辑它们以便我只看到圆圈?请帮忙。我是 quartz 2d 新手

这是我在 Controller 的 viewDidLoad 中调用它的方式

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    Bubble *bub = [[Bubble alloc]initWithFrame:CGRectMake(210.0, 90.0, 60.0, 60.0)];
    [self.view addSubview:bub];
    [bub release];
}

最佳答案

把这个设为viewDidLoad

bub.backgroundColor = [UIColor clearColor];

关于ios - 用quartz2d绘图时如何将矩形夹在圆周围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237580/

相关文章:

macos - 使用 CGEventTapCreate Swift 中的参数问题

ios - 构建起始代码时 Apple Mach-O 链接器错误

ios - 401错误-使用AFNetworking AFHTTPClient和Tastypie进行基本身份验证

iphone - iOS Interface Builder 将 View 锁定在其表格单元格的右侧

iphone - Quartz 2D/OpenGLES 图像上的几何变形(最好使用 CGImage)

ios - 带有 CGPath 的 UIView 和另一个包含第一个 UIView 的 UIView。如何使第一个 View 适合第二个 View ?

macos - 在 Mac 上截取特定应用程序窗口的屏幕截图

ios - React Native Image 有时无法工作

ios - 应该如何分发使用 VOIP 标志始终保持连接的 b2b 应用程序

iphone - 使用 Quartz 在 iOS 上获取 PDF 超链接