- (void)drawRect:(CGRect)rect {
CGContextRef content = UIGraphicsGetCurrentContext();
//1.绘制图画
// [self drawImage:content];
//2.绘制文字
// [self drawtext:content];
//3.绘制贝塞尔曲线
// [self drawCure:content];
//4.绘制圆弧
[self drawArc:content];
}
#pragma mark - 绘制图画
- (void)drawImage:(CGContextRef)content {
UIImage *image = [UIImage imageNamed:@"2012100413195471481.jpg"];
//转换坐标
CGContextSaveGState(content);
CGContextRotateCTM(content, M_PI);
CGContextScaleCTM(content, -1, 1);
CGContextTranslateCTM(content, 0, -self.bounds.size.height);
CGContextDrawImage(content, self.bounds, image.CGImage);
CGContextRestoreGState(content);
}
#pragma mark - 绘制文字
- (void)drawtext:(CGContextRef)content {
NSString *str = @"哈哈哈";
CGRect rect = CGRectMake(50, 50, 200, 200);
[[UIColor redColor] setFill];
UIRectFill(rect);
CGRect rect1 = CGRectMake(200, 50, 200, 200);
[[UIColor orangeColor] setFill];
UIRectFill(rect1);
UIFont *font = [UIFont systemFontOfSize:20];
[str drawInRect:rect withAttributes:@{NSFontAttributeName:font}];
}
#pragma mark - 绘制贝塞尔曲线
- (void)drawCure:(CGContextRef)context {
CGContextSetLineWidth(context, 5);
[[UIColor redColor] setStroke];
CGContextMoveToPoint(context, 20, 100);
CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);
CGContextDrawPath(context, kCGPathStroke);
}
#pragma mark - 绘制圆弧
- (void)drawArc:(CGContextRef)context {
//转换坐标
CGContextSaveGState(context);
CGContextRotateCTM(context, M_PI);
CGContextScaleCTM(context, -1, 1);
CGContextTranslateCTM(context, 0, -self.bounds.size.height);
[[UIColor grayColor] setFill];
[[UIColor redColor] setStroke];
CGPoint centerPoint = CGPointMake(150, 150);
float radius = 100;
CGFloat beginAngle = 0;
CGFloat endAngle = M_PI_4;
CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, beginAngle, endAngle, 1);
CGContextMoveToPoint(context, centerPoint.x+radius*cos(beginAngle), centerPoint.y+radius*sin(beginAngle));
CGContextAddLineToPoint(context, centerPoint.x, centerPoint.y);
CGContextAddLineToPoint(context, centerPoint.x+radius*cos(endAngle), centerPoint.y+radius*sin(endAngle));
CGContextDrawPath(context, kCGPathEOFillStroke);
}