问题描述
想要创建一个在其中具有透明框架的视图,以便可以通过此透明框架看到视图后面的视图,但是此外的区域将无法显示。所以在视图中基本上是一个窗口。
Looking to create a view that has a transparent frame inside of it so that the views behind the view can be seen through this transparent frame, but areas outside of this will not show through. So essentially a window within the view.
希望能够做到这样的事情:
Hoping to be able to do something like this:
CGRect hole = CGRectMake(100, 100, 250, 250);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);
CGContextAddRect(context, hole);
CGContextClip(context);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
但是clear不会覆盖黑色,所以整个背景都是黑色。沿着这些方向的任何想法?
but the clear does not override the black so whole background is black. Any ideas along these lines?
推荐答案
这是我的实现(因为我确实需要透明部分的视图):
This is my implementation (as I did needed a view with transparent parts):
标题(.h)文件:
// Subclasses UIview to draw transparent rects inside the view
#import <UIKit/UIKit.h>
@interface PartialTransparentView : UIView {
NSArray *rectsArray;
UIColor *backgroundColor;
}
- (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor*)color andTransparentRects:(NSArray*)rects;
@end
实施(.m)档案:
#import "PartialTransparentView.h"
#import <QuartzCore/QuartzCore.h>
@implementation PartialTransparentView
- (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor*)color andTransparentRects:(NSArray*)rects
{
backgroundColor = color;
rectsArray = rects;
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.opaque = NO;
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[backgroundColor setFill];
UIRectFill(rect);
// clear the background in the given rectangles
for (NSValue *holeRectValue in rectsArray) {
CGRect holeRect = [holeRectValue CGRectValue];
CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );
[[UIColor clearColor] setFill];
UIRectFill(holeRectIntersection);
}
}
@end
现在要添加具有部分透明度的视图,您需要导入PartialTransparentView自定义UIView子类,然后按如下方式使用它:
Now to add a view with partial transparency, you need to import the PartialTransparentView custom UIView subclass, then use it as follows:
NSArray *transparentRects = [[NSArray alloc] initWithObjects:[NSValue valueWithCGRect:CGRectMake(0, 50, 100, 20)],[NSValue valueWithCGRect:CGRectMake(0, 150, 10, 20)], nil];
PartialTransparentView *transparentView = [[PartialTransparentView alloc] initWithFrame:CGRectMake(0,0,200,400) backgroundColor:[UIColor colorWithWhite:1 alpha:0.75] andTransparentRects:rects];
[self.view addSubview:backgroundView];
这将创建一个包含2个透明rects的视图。
当然,你可以添加任意数量的rects,或者只使用一个。
上面的代码只处理矩形,所以如果你想使用圆圈,你必须修改它。
This will create a view with 2 transparent rects.Of course you can add as many rects as you wish, or just use one.The above code is only handling rectangles, so if you wish to use circles, you will have to modify it.
这篇关于在UIView中剪切透明孔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!