iDev

Draw Simple line on iPhone app

KraZYeom 2009. 1. 7. 09:55
반응형
Drawing a line is very easy indeed, the "problem" is that drawing a line is an ill defined "request", you need to decide what color you want your line, what width etc.
Anyhow,
in your view's drawRect method (assuming you're subclassing a view, that is you have a class that inherits UIView and you are making an instance of this class viewable):
in drawRect:
CGContextRef ctx = UIGraphicsGetCurrentContext(); //get the graphics context
CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1); //there are two relevant color states, "Stroke" -- used in Stroke drawing functions and "Fill" - used in fill drawing functions
//now we build a "path"
you can either directly build it on the context or build a path object, here I build it on the context
CGContextMoveToPoint(ctx, 0, 0);
//add a line from 0,0 to the point 100,100
CGContextAddLineToPoint( ctx, 100,100);
//"stroke" the path
CGContextStrokePath(ctx);


중요한건 UIView에서 상속 받아야한다는것입니다. 그리고 기본적으로 생성되는 drawRect안에 설정.

계속에서 뭔가 에러가 났었는데 그 이유가 UIView를 상속 안받아서 그런듯하네요. 
반응형

'iDev' 카테고리의 다른 글

Hide Status Bar  (0) 2009.01.23
W-8BEN form을 E-Mail로 보내세요!  (0) 2009.01.16
NSInteger to NSString in Obj-C  (0) 2009.01.14
[개발] iPod touch인지 iPhone 인지 구분하기  (0) 2009.01.13
drawRect: 와 setNeedsDisplay  (2) 2009.01.10