您的位置:首页 > 移动开发 > IOS开发

iOS drawRect绘画

2015-10-29 09:14 351 查看
绘画只在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] setStrok
4000
e]代替;
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