开发者

basic question about coregraphics

Im working on a project with out Interface Builder,

i want to draw a line and a test square, but is not working here the code

- (void)drawRect:(CGRect)rect {
 CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
 //Set the stroke (pen) color
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
//Set the width of the pen mark
CGContextSetLineWidth(context, 5.0);
  //Start at this point
//CGContextMoveToPoint(context, 10.0, 30.0);
CGContextMoveToPoint(context, 20, 20);
  //Give instructions to the CGContext
//(move "pen" around the screen)
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
 // Draw a red solid square
CGContextSetRGBFillColor(context, 255, 0, 0, 1);
CGContextFillRect(context, CGRectMake(10, 10, 50, 50));
 }
 - (void)viewDidLoad
 {
[super viewDidLoad];
 self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor grayColor];
   UILabel *labelA = [[[UILabel 开发者_Python百科alloc] initWithFrame:CGRectMake(100, 100, 100, 20)]autorelease];
      labelA.text = @"mk ss9";
[self.view addSubview:labelA]; 
}

the label shows fine, but the line and square are not showing, what is missing??

thanks a lot!


Ok got it!

the problem is that I was attempting to draw in my ViewController, and that is not the way,

So I created a new class, with subclass UIView, and it works fine now!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜