#import "TestView.h"
@implementation TestView
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Initialization code
}
return self;
}
-(IBAction)Line
{
mode = 1;
[self setNeedsDisplay];
}
-(IBAction)Rect
{
mode = 2;
[self setNeedsDisplay];
}
-(IBAction)Polygon
{
mode = 3;
[self setNeedsDisplay];
}
-(void) drawLine:(CGContextRef)context
{
// 선 색상 설정
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// 선 두께
CGContextSetLineWidth(context, 1.0);
// 선 그리기
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
CGContextSetLineWidth(context, 1.0);
// 두번째 선
CGContextSetLineWidth(context, 4.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(context, 10.0, 50.0);
CGContextAddLineToPoint(context, 310.0, 50.0);
CGContextStrokePath(context);
// 점선
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGFloat dash[] = {5.0, 10.0, 15.0, 20.0};
CGContextSetLineDash(context, 0.0, dash, 4);
CGContextMoveToPoint(context, 10.0, 70.0);
CGContextAddLineToPoint(context, 310.0, 70.0);
CGContextStrokePath(context);
CGContextSetLineDash(context, 0.0, NULL, 0); // 점선을 그리게 해주는 방법
// 배열 이용하기
CGPoint lines[] =
{
CGPointMake(10.0, 90.0),
CGPointMake(60.0, 140.0),
CGPointMake(110.0, 130.0),
CGPointMake(160.0, 190.0),
CGPointMake(210.0, 200.0),
CGPointMake(260.0, 90.0),
CGPointMake(310.0, 200.0),
};
CGContextSetLineWidth(context, 5.0);
CGContextAddLines(context, lines, sizeof(lines)/sizeof(lines[0])); // 배열의 갯수 구하는 방법이지만
// 포인터 배열에서는 사용하지 말 것.
CGContextStrokePath(context);
}
-(void) drawRectangle:(CGContextRef)context
{
// 선 색상
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// 채우기 색
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
// 선 두께
CGContextSetLineWidth(context, 2.0);
// 경로에 사각형 추가
CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
CGContextStrokePath(context);
// 사각형 채우기
CGContextFillRect(context, CGRectMake(130.0, 30.0, 60.0, 60.0));
CGContextStrokeRectWithWidth(context, CGRectMake(230.0, 30.0, 60.0, 60.0), 4.0);
}
-(void) drawPolygon:(CGContextRef)context
{
CGPoint center;
// 선 색상
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// 채우기 색
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
// 선 두께
CGContextSetLineWidth(context, 1.0);
// 별 그리기
center = CGPointMake(80.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for(int i = 1; i < 5; ++i)
{
CGFloat x = 30.0 * sinf(i * 4.0 * M_PI / 5.0); // M_PI -> Objective-C에서 매크로로 정의해둔
// 원주율 값
CGFloat y = 30.0 * cosf(i * 4.0 * M_PI / 5.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);
}
CGContextClosePath(context);
CGContextFillPath(context);
// 별 + 원 그리기
center = CGPointMake(160.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for(int i = 1; i < 5; ++i)
{
CGFloat x = 30.0 * sinf(i * 4.0 * M_PI / 5.0);
CGFloat y = 30.0 * cosf(i * 4.0 * M_PI / 5.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);
}
CGContextAddEllipseInRect(context,CGRectMake(130, 70, 60,60));
CGContextClosePath(context);
CGContextEOFillPath(context);
// 5각형 그리기
center = CGPointMake(240.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for(int i = 1; i < 6; ++i)
{
CGFloat x = 30.0 * sinf(i * 2.0 * M_PI / 6.0);
CGFloat y = 30.0 * cosf(i * 2.0 * M_PI / 6.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);
}
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
if(mode==1)
[self drawLine:context];
else if(mode==2)
[self drawRectangle:context];
else
[self drawPolygon:context];
// Drawing code
}