iphone 在UIView上畫圖,畫線,畫多邊形
阿新 • • 發佈:2019-02-05
1.自定義一個 CustomView : UIView類,該類繼承自 UIView,當然也可以繼承自UIView的子類,比如 UIScrollView
2.在CustomView中重寫下面的方法,這個方法中的內容繪製程式碼,可以參考 iphone Quartz 2D 開發指南.
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0); // Draw them with a 2.0 stroke width so they are a bit more visible. CGContextSetLineWidth(context, 2.0); for(int idx = 0; idx < self.points.count; idx++) { point = [self.points objectAtIndex:idx];//Edited if(idx == 0) { // move to the first point CGContextMoveToPoint(context, point.x, point.y); } else { CGContextAddLineToPoint(context, point.x, point.y); } } CGContextStrokePath(context); }
3.在xib中的拖動一個UIView到介面中,將這個UIView的 Class屬性設定成 CustomView即可.