iphone - 设置CGContext透明背景

标签 iphone graphics background transparent cgcontext

我仍在努力与 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);
}

有什么想法吗?

最佳答案

UIGraphicsGetCurrentContext()调用CGContextClearRect(context,rect)

之后

编辑: 好的,明白了。

带有该行的自定义 View 应具有以下内容:

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

关于iphone - 设置CGContext透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2125543/

相关文章:

html - 创建具有对 Angular 线背景颜色的全宽标题

html - 在不影响导航栏或文本的情况下将背景图像变灰

ios - @3x 图像应该比原始 iPhone 图像大 3 倍还是 4 倍?

java - 图形窗口中的基本弹跳球

graphics - 网络播放器 GUI 渲染问题像素化

java - 将自定义 View 添加到 xml 布局

iphone - NSXMLParser 值不被保留

iphone - objective-c 开关语句中的OR运算符

iOS 我可以将所有委托(delegate)方法放在另一个类上吗?将委托(delegate)继承到 View Controller 中

ios - 应用代表 : Service call coming back from background