ios 在 UIView 中绘制矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15082699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:27:38  来源:igfitidea点击:

Drawing a rectangle in UIView

ioscore-graphics

提问by Ashish Agarwal

I am trying to draw a transparent rectangle in my UIView which has a black border.

我想在我的 UIView 中绘制一个透明矩形,它有一个黑色边框。

My code however creates a completely black rectangle. Here's my code so far:

然而,我的代码创建了一个完全黑色的矩形。到目前为止,这是我的代码:

- (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);

}

回答by Guo Luchuan

- (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

}

the effect is like this (backgroundColor is blue)

效果是这样的(backgroundColor为蓝色)

enter image description here

在此处输入图片说明

回答by Madhu

It will provide the rectangle/square by adjusting values of self frame (customized view or subclass of any class which is inherited from UIView) with transparency.

它将通过调整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];

回答by Imanou Petit

Your code does not require the CGContextSetRGBFillColorcall and is missing the CGContextStrokeRectcall. With Swift 5, your final draw(_:)implementation should look like this:

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

}


As an alternative, if your really want to call CGContextSetRGBFillColor, you also to have call CGContextFillRect. Your final draw(_:)implementation would then look like this with Swift 3:

作为替代方案,如果您真的想拨打CGContextSetRGBFillColor,您也可以拨打CGContextFillRectdraw(_:)使用 Swift 3,您的最终实现将如下所示:

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)
    }

}

回答by Fattie

One handy tip ......

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

Very often, when you need to draw a square

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

it's easier to just draw a 'thick stripe'.....

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

    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()

That will draw a square, which runs downwards from 200 to 300.

这将绘制一个正方形,它从 200 向下延伸到 300。

It's centered on 500 across, and is 100 wide.

它以 500 为中心,宽度为 100。

回答by Madhu

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);