在UIView中绘制矩形

18
我正在尝试在具有黑色边框的UIView中绘制一个透明矩形。 然而,我的代码创建了一个完全黑色的矩形。以下是我目前的代码:
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rectangle = CGRectMake(0, 100, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);

}

1
CGContextFillRect更改为CGContextStrokeRect - Eric Qian
相同的问题。只有一个黑色的矩形(而且完全是黑色的)。 - Ashish Agarwal
尝试在CGContextSetRGBFillColor(context,0.0,0.0,0.0,0.5)中使用alpha 0.0。 - Exploring
你是想要创建一个透明的窗口吗?看看这个链接是否有帮助:https://dev59.com/KmbWa4cB1Zd3GeqPbN6W#14645130 - Gopal R
5个回答

21
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rectangle = CGRectMake(0, 100, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);   //this is the transparent color
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.5);
    CGContextFillRect(context, rectangle);
    CGContextStrokeRect(context, rectangle);    //this will draw the border

}

效果如下(背景颜色为蓝色)

enter image description here


哦,它什么都没做。即使我尝试输入一些随机的 RGB 值,它每次都只显示一个黑色矩形。 - Ashish Agarwal
@AshishAgarwal,你想要透明的矩形,为什么要输入随机的RGB值?看看我的绘图。 - Guo Luchuan
1
嗯,我正在尝试看到除黑色以外的东西。但是到目前为止没有成功。 - Ashish Agarwal
@Guo Luchuan:我们能否设置这个矩形的Alpha属性,使得它变成50%透明?谢谢。 - Job_September_2020

4

您的代码不需要调用 CGContextSetRGBFillColor,而且缺少了 CGContextStrokeRect 的调用。使用Swift 5,您的最终 draw(_:) 实现应该如下所示:

class CustomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else { return }    
        ctx.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        let rectangle = CGRect(x: 0, y: 100, width: 320, height: 100)
        ctx.stroke(rectangle)
    }

}

作为另一种选择,如果你真的想要调用 CGContextSetRGBFillColor,你也需要调用 CGContextFillRect。使用 Swift 3,你最终的 draw(_:) 实现将如下所示:
class CustomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else { return }

        ctx.setFillColor(red: 1, green: 1, blue: 1, alpha: 0)
        ctx.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 0.5)

        let rectangle = CGRect(x: 0, y: 100, width: 320, height: 100)
        ctx.fill(rectangle)
        ctx.stroke(rectangle)
    }

}

3

通过调整自定义视图或从UIView继承的任何类的子类的self frame的值,它将提供带有透明度的矩形/正方形。

[self.layer setBorderWidth:1.0];
[self.layer setBorderColor:[[UIColor colorWithRed:0.10 green:0.45 blue:0.73 alpha:1.0] CGColor]];
[self.layer setCornerRadius:2.0];
[self.layer setShadowOffset:CGSizeMake(-2, -2)];
[self.layer setShadowColor:[[UIColor lightGrayColor] CGColor]];
[self.layer setShadowOpacity:0.5];

我们能否为这个矩形设置Alpha属性,使得这个矩形变成50%的透明度?谢谢。 - Job_September_2020

1

一个方便的技巧是,当你需要画一个正方形时,更容易的方法是只需画一个"粗条纹"......

    let context = UIGraphicsGetCurrentContext()
    context!.setLineWidth(100)
    context!.setStrokeColor(blah.cgColor)
    context?.move(to: CGPoint(x: 500, y: 200))
    context?.addLine(to: CGPoint(x: 500, y: 300))
    context!.strokePath()

这将绘制一个正方形,从200到300向下运行。

它在500的中心位置,宽度为100。


0
CGRect  bounds  = connectorRect;
CGFloat minx = CGRectGetMinX(bounds), midx = CGRectGetMidX(bounds), maxx = CGRectGetMaxX(bounds);
CGFloat miny = CGRectGetMinY(bounds), maxy = CGRectGetMaxY(bounds);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context,[UIColor clearColor].CGColor);
CGContextMoveToPoint(context, minx, maxy);
CGContextAddLineToPoint(context, midx, miny);
CGContextAddLineToPoint(context, maxx, maxy);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

3
这是给三角形小伙子而不是长方形的。 - ashokdy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接