OGeek|极客世界-中国程序员成长平台

标题: ios - CGContext:位图背景颜色:无法设置为白色:IOS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:15
标题: ios - CGContext:位图背景颜色:无法设置为白色:IOS

我正在尝试修改一些简单的“创建绘画应用程序”代码,使其具有白色背景色,而不是设置为的黑色。示例代码位于:

http://blog.effectiveui.com/?p=8105

我尝试设置 self.backgroundcolor = [UIColor whiteColor],也设置 [[UIColor whiteColor] setFill] 没有效果。由于缺乏经验,我错过了一些非常基本的东西。

有人有什么想法吗?非常感谢!



Best Answer-推荐答案


我在 drawRect 中添加了几行应该为你做的事情。您在正确的轨道上,但是当您将颜色设置为白色时,您实际上需要用它填充 paintView 矩形:

- (void)drawRectCGRect)rect {
    if(touch != nil){
        CGContextRef context = UIGraphicsGetCurrentContext();

        //clear background color to white
        [[UIColor whiteColor] setFill];
        CGContextFillRect(context, rect);

        hue += 0.005;
        if(hue > 1.0) hue = 0.0;
        UIColor *color = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0];

        CGContextSetStrokeColorWithColor(context, [color CGColor]);
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, 15);

        CGPoint lastPoint = [touch previousLocationInView:self];
        CGPoint newPoint = [touch locationInView:self];

        CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(context, newPoint.x, newPoint.y);
        CGContextStrokePath(context);
    }
}

原因 view.backgroundColor = [UIColor whiteColor];不起作用的是任何实现 drawRect 的 View 都忽略了 backgroundColor 属性,程序员负责绘制整个 View 内容,包括背景。

关于ios - CGContext:位图背景颜色:无法设置为白色:IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130030/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4