iOS drawRect绘画

绘画只在UIView中执行,需要新建UIView的子类

#if0
// 画一条线
- (void)drawRect:(CGRect)rect{
    CGContextRef ref = UIGraphicsGetCurrentContext(); // 拿到当前画板,在这个画板上画就是在视图上画
    CGContextBeginPath(ref); // 开始绘画
   
    CGContextMoveToPoint(ref, 0, 0); // 画线
    CGContextAddLineToPoint(ref, 300, 300);
   
    CGFloat redColor[4] = {1.0, 0, 0, 1.0};
    CGContextSetStrokeColor(ref, redColor); // 设置当前画笔的颜色,这两句可以用[[UIColor whiteColor] setStroke]代替;
    CGContextStrokePath(ref); // 对移动的路径画线
}
#endif
 
#if 1
// 画三角
- (void)drawRect:(CGRect)rect{
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ref, 0.5, 0.5, 0.5, 1.0);
    CGContextSetLineWidth(ref, 3.0); // 让线条变粗
    CGPoint points[] = { // 设置四个点画三条线让线连起来
        CGPointMake(100, 100),
        CGPointMake(50, 300),
        CGPointMake(300, 500),
        CGPointMake(100, 100),
    };
    CGContextAddLines(ref, points, sizeof(points) / sizeof(points[0]));
    CGFloat redColor[4] = {1.0, 0, 0, 1.0};   
    CGContextSetFillColor(ref, redColor); // 填充颜色,这两句可使用[[UIColor redColor] setFill];
    CGContextDrawPath(ref, kCGPathFillStroke); // 画填充的图案
 
}
#endif
 

你可能感兴趣的:(iOS drawRect绘画)