17

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

}
4

5 に答える 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

}

効果はこんな感じです(backgroundColorは青)

ここに画像の説明を入力してください

于 2013-02-26T06:32:55.303 に答える
3

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];
于 2013-02-26T06:46:18.387 に答える
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)
    }

}
于 2017-04-08T21:38:42.970 に答える
1

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です。

于 2017-07-19T23:34:58.457 に答える
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);
于 2013-02-26T06:50:17.633 に答える