【问题标题】:Drawing a rectangle in UIView在 UIView 中绘制一个矩形
【发布时间】:2013-02-26 06:22:42
【问题描述】:

我试图在我的 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);

}

【问题讨论】:

  • CGContextFillRect 更改为CGContextStrokeRect
  • 同样的问题。只有一个黑色矩形(而且完全是黑色的)
  • 尝试在 CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.5) 中使用 alpha 0.0;
  • 您是否尝试使用此视图创建透明的透视窗口?看看这是否有帮助stackoverflow.com/questions/11792563/…

标签: ios core-graphics


【解决方案1】:
- (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

}

效果是这样的(背景颜色为蓝色)

【讨论】:

  • 嗯,它什么也没做。即使我尝试为 RGB 输入一些随机值,它也只是每次都显示一个黑色矩形。
  • @AshishAgarwal 你想要透明矩形,为什么你要为 RGB 输入随机值?看我的图
  • 好吧,我想看看黑色以外的东西。但到目前为止没有运气
  • @郭陆川:我们能不能也给这个矩形设置Alpha属性,让这个矩形50%透明?谢谢。
【解决方案2】:

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

[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% 透明?谢谢。
【解决方案3】:

您的代码不需要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)
    }

}

【讨论】:

    【解决方案4】:

    一个方便的提示......

    很多时候,当你需要画一个正方形时

    只画一条“粗条纹”会更容易 .....

        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。

    【讨论】:

      【解决方案5】:
      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);
      

      【讨论】:

      • 这是三角形的,不是矩形的
      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多