cocoa - 如何从其父 View 中删除带有圆角的 UIView?

标签 cocoa cocoa-touch rounded-corners quartz-2d cgcontext

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

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

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

self.layer.cornerRadius = 4.25;

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

最佳答案

这就是我最终让它发挥作用的方法。这会产生一个与“洞”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);
}

关于cocoa - 如何从其父 View 中删除带有圆角的 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4349400/

相关文章:

objective-c - 如何检测 NSDrawer 是否会在屏幕外打开

macos - OSX/macOS - 如何将 xib 迁移到 Storyboard

iphone - iPhone 上的手机 ID 信息

qt - QML : Rounded rectangle with border

xcode - 如何在 XCode 3.2.6 中将 CLI 目标添加到 GUI 项目

swift - Cocoa 中的多个撤消管理器?

ios - 如何回到 Root View Controller

iphone - Obj-c,在没有核心数据的情况下,执行许多 SQLite 插入/更新查询的最快方法是什么?

html - CSS 3's border-radius property and border-collapse:collapse don' t 混合。如何使用border-radius创建一个圆 Angular 折叠表?