2013-02-26 42 views
14

Tôi đang cố vẽ hình chữ nhật trong suốt trong UIView có viền đen.Vẽ hình chữ nhật ở UIView

Mã của tôi tuy nhiên tạo ra một hình chữ nhật hoàn toàn màu đen. Dưới đây là mã của tôi cho đến nay:

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

Thay đổi 'CGContextFillRect' thành' CGContextStrokeRect' –

+0

Cùng một vấn đề. Chỉ có một hình chữ nhật màu đen (và nó hoàn toàn là màu đen) –

+0

thử với alpha 0.0 trong CGContextSetRGBFillColor (ngữ cảnh, 0.0, 0.0, 0.0, 0.5); – Exploring

Trả lời

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

} 

hiệu quả là như thế này (backgroundColor là màu xanh)

enter image description here

+0

Hmm, nó không làm gì cả. Ngay cả khi tôi đã thử nhập một số giá trị ngẫu nhiên cho RGB, nó chỉ hiển thị hình chữ nhật màu đen mọi lúc. –

+0

@AshishAgarwal bạn muốn hình chữ nhật trong suốt, tại sao bạn nhập giá trị ngẫu nhiên cho RGB? nhìn vào bản vẽ của tôi –

+1

Vâng, tôi đang cố gắng để xem một cái gì đó khác hơn là màu đen. Nhưng không may mắn cho đến nay –

3

Nó sẽ cung cấp các hình chữ nhật/vuông bằng cách điều chỉnh giá trị của khung tự (tùy chỉnh xem hoặc phân lớp của bất kỳ lớp nào được kế thừa từ UIView) với độ trong suốt.

[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]; 
+0

Chúng ta có thể thiết lập thuộc tính Alpha cho hình chữ nhật này để hình chữ nhật này có thể là 50% trong suốt không? Cảm ơn bạn. – Thanh

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

Đây là hình tam giác Dude không cho Hình chữ nhật được yêu cầu Hình chữ nhật – ashokdy

+1

nhưng u hiển thị hình tam giác – vishnu

1

Mã của bạn không yêu cầu CGContextSetRGBFillColor cuộc gọi và thiếu CGContextStrokeRect gọi. Với Swift 3, thức draw(_:) thực hiện của bạn sẽ trông như thế này:

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

} 

Là một thay thế, nếu bạn thực sự muốn gọi CGContextSetRGBFillColor, bạn cũng có cuộc gọi CGContextFillRect. thức draw(_:) thực hiện của bạn sau đó sẽ trông như thế này với 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) 
    } 

} 
1

Một mẹo nhỏ ......

Rất thường, khi bạn cần phải vẽ một hình vuông

nó dễ dàng hơn để chỉ cần vẽ một 'sọc dày' .....

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

Điều đó sẽ vẽ một hình vuông, chạy xuống từ 200 đến 300.

Nó được đặt ở giữa trên 500, và rộng 100.

Các vấn đề liên quan