我仍然在努力与CGContext划清界限。我实际上已经去画线,但现在我需要矩形的背景是透明的,以便现有的背景显示通过。下面是我的测试代码:
(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetAlpha(context,0.0);
CGContextFillRect(context, rect);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, 100.0,0.0);
CGContextAddLineToPoint(context,100.0, 100.0);
CGContextStrokePath(context);
}
有什么想法吗?
发布于 2010-01-24 09:45:52
在UIGraphicsGetCurrentContext()
之后调用CGContextClearRect(context,rect)
编辑:好的,明白了。
包含行的自定义视图应具有以下内容:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, 100.0,0.0);
CGContextAddLineToPoint(context,100.0, 100.0);
CGContextStrokePath(context);
}
我的测试使用了这个非常基本的UIViewController:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
[v setBackgroundColor:[UIColor redColor]];
[self.view addSubview:v];
TopView *t = [[TopView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:t];
[v release];
[t release];
}
发布于 2012-01-28 22:17:48
简单的方法:
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
self.opaque = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
//your code
}
发布于 2017-06-22 18:37:26
使用opaque == false
初始化上下文,Swift 3
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
不透明
指示位图是否不透明的布尔标志。如果知道位图是完全不透明的,请指定true以忽略alpha通道并优化位图的存储。指定false意味着位图必须包含一个alpha通道来处理任何部分透明的像素。
https://stackoverflow.com/questions/2125543
复制相似问题