2015-08-28 29 views
0

使用DrawRect方法繪製一個矩形視圖。我必須設置該特定矩形視圖的半徑拐角。我寫下了一個矩形視圖。如何使用DrawRect方法設置要查看的半徑角?

- (void)drawRect:(CGRect)rect { 
int coordinate_x=60,coordinate_y=120,width=200,height=295; 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGRect drawTank = {coordinate_x, coordinate_y, width, height}; 

CGColorRef blackColor=[UIColor blackColor].CGColor; 

CGContextSetRGBStrokeColor(context, 0, 0, 255, 1); 

CGContextSetLineWidth(context, 1.0); 

CGContextSetStrokeColorWithColor(context, blackColor); 

CGContextStrokeRect(context, drawTank); 
} 

而我必須使用下面的填充視圖和空視圖使用代碼。

CGRect emptyTank = CGRectMake(coordinate_x, coordinate_y, width, height *(1-filledPercentage)); 

CGRect fullTank = CGRectMake(coordinate_x, coordinate_y+height *(1-filledPercentage),width, height * filledPercentage); 

如何設置特定視圖的半徑拐角。請幫幫我。

在此先感謝。

回答

0

,你可以簡單地設置一個UIView與圓角半徑:

view.layer.cornerRadius = 5; 

如果使用beizer路徑,你可以設置角半徑用下面的代碼:

UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(55, 30, 133, 51) cornerRadius: 5]; 
[UIColor.grayColor setFill]; 
[rectanglePath fill]; 

中的drawRect內方法:

- (void)drawRect: (CGRect)frame 
{ 

    //// Rectangle Drawing 
    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(CGRectGetMinX(frame) + 40, CGRectGetMinY(frame) + 21, 133, 51) cornerRadius: 7]; 
    [UIColor.grayColor setFill]; 
    [rectanglePath fill]; 
} 
+0

請發送完整的代碼來繪製半徑角的視圖。 –

+0

我的答案有代碼,它有兩種方法可以做到這一點。你喜歡哪一個?以編程方式創建uiview?或從sotryboard創建它? –