本文介绍了如何从其父视图中删除带有圆角的UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为3.2和更高版本创建iPad应用程序。我的应用程序有一个重叠视图,具有半透明度,使下面的一切更暗。在这个视图的中间,我在这个半透明的一个洞,让一部分的背景过滤器通过未受伤害的代码:

I'm creating an iPad app for 3.2 and later. My app has an overlay view which has a semi-transparency that makes everything below it darker. In the middle of this view I am chopping a hole in this semi-transparency to let part of the background filter through unscathed, with this code:

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

此外,'hole'视图有圆角, p>

Additionally, the 'hole' view has rounded corners, applied via:

self.layer.cornerRadius = 4.25;

这是一个很好的工作,除了一个小问题 - 这些圆角不考虑,被砍掉的有方角,而不是圆角。我需要解决这个问题,但我不知道如何。任何想法,例子,想法?

This works great except for one small problem - these rounded corners are not taken into account, so the hole that gets chopped out has square corners instead of rounded. I need to fix this, but I have no idea how. Any ideas, examples, thoughts?

推荐答案

这里是我最后得到它的工作。这产生一个与'hole'UIView具有相同框架的孔,从自己(UIView)剪切出来。这可以让你看到后面的洞不受阻碍。

Here's how I ended up getting it to work. This produces a hole with the same frame as the 'hole' UIView, cutting it out from self (UIView). This lets you see whatever is behind the 'hole' unhindered.

- (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);
}

这篇关于如何从其父视图中删除带有圆角的UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 17:55