iOS 使用drawRect: 繪制虛線橢圓
1:首先如果要使用 drawRect 繪圖
要導入 CoreGraphics.framework 框架
然后 創建 自定義view, 即是 myView繼承 UIView;
2: 重寫
- (void)drawRect:(CGRect)rect
方法;
3:添加如下代碼
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat lengths[] = {5,5,5,5}; CGRect aRect= CGRectMake(60, 1,self.bounds.size.width-60*2,self.bounds.size.height-4); CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); CGContextSetLineDash(context, 0, lengths, 4); CGContextSetLineWidth(context, 3.0); CGContextAddEllipseInRect(context, aRect); //橢圓 CGContextDrawPath(context, kCGPathStroke); }
主要函數
CGContextSetRGBStrokeColor(context, r, g, b, 1.0); 設置圖形線的顏色;
CGContextSetLineDash(context, 0, lengths, 4); 設置線條為 虛線;
CGContextSetLineWidth(context, 3.0); //設置線寬;
CGContextAddEllipseInRect(context, aRect); //畫橢圓
CGContextDrawPath(context, kCGPathStroke);