1

我正在为 3.2 及更高版本创建一个 iPad 应用程序。我的应用程序有一个覆盖视图,它具有半透明性,使其下方的所有内容都变暗。在这个视图的中间,我在这个半透明的地方切了一个洞,让部分背景过滤器毫发无损地通过,代码如下:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect intersection = CGRectIntersection(hole.frame, rect);
    CGContextClearRect(context, intersection);
}

此外,“洞”视图具有圆角,通过以下方式应用:

self.layer.cornerRadius = 4.25;

除了一个小问题之外,这很有效——这些圆角没有被考虑在内,所以被切掉的孔有方角而不是圆角。我需要解决这个问题,但我不知道如何解决。有什么想法、例子、想法吗?

4

3 回答 3

3

这就是我最终让它工作的方式。这会产生一个与“洞”UIView 具有相同框架的洞,将其从自身 (UIView) 中切出。这让您可以畅通无阻地看到“洞”后面的任何东西。

- (void)drawRect:(CGRect)rect {
    CGFloat radius = self.hole.layer.cornerRadius;
    CGRect c = self.hole.frame;
    CGContextRef context = UIGraphicsGetCurrentContext();

        // this simply draws a path the same shape as the 'hole' view
    CGContextMoveToPoint(context, c.origin.x, c.origin.y + radius);
    CGContextAddLineToPoint(context, c.origin.x, c.origin.y + c.size.height - radius);
    CGContextAddArc(context, c.origin.x + radius, c.origin.y + c.size.height - radius, radius, M_PI_4, M_PI_2, 1);
    CGContextAddLineToPoint(context, c.origin.x + c.size.width - radius, c.origin.y + c.size.height);
    CGContextAddArc(context, c.origin.x + c.size.width - radius, c.origin.y + c.size.height - radius, radius, M_PI_2, 0.0f, 1);
    CGContextAddLineToPoint(context, c.origin.x + c.size.width, c.origin.y + radius);
    CGContextAddArc(context, c.origin.x + c.size.width - radius, c.origin.y + radius, radius, 0.0f, -M_PI_2, 1);
    CGContextAddLineToPoint(context, c.origin.x + radius, c.origin.y);
    CGContextAddArc(context, c.origin.x + radius, c.origin.y + radius, radius, -M_PI_2, M_PI, 1);

        // finish
    CGContextClosePath(context);
    CGContextClip(context); // this is the secret sauce
    CGContextClearRect(context, c);
}
于 2010-12-09T20:57:45.860 回答
1

您正在尝试做的事情称为掩蔽。您可以使用 Core Graphics 来屏蔽当前的图形上下文。在此处查看 Apple 关于该主题的文档:

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBHDDBE

于 2010-12-03T23:00:31.133 回答
0

如果您更改cornerRadius图层的属性,您还必须在关联视图上设置clipsToBoundsYES,以便将内容剪辑到圆角。

于 2010-12-03T21:48:48.293 回答