关于 coregraphics 的基本问题
我正在处理一个没有 Interface Builder 的项目,
我想画一条线和一个测试方块,但不起作用 这是代码
- (void)drawRect:(CGRect)rect { CGContextRef 上下文 = UIGraphicsGetCurrentContext(); CGContextClearRect(上下文, 矩形); //设置描边(笔)颜色 CGContextSetStrokeColorWithColor(上下文, [UIColor blueColor].CGColor); //设置笔标记的宽度 CGContextSetLineWidth(上下文, 5.0); //从此时开始 //CGContextMoveToPoint(上下文, 10.0, 30.0); CGContextMoveToPoint(上下文, 20, 20); //给CGContext下指令 //(在屏幕上移动“笔”) CGContextAddLineToPoint(上下文, 310.0, 30.0); CGContextStrokePath(上下文); // 绘制一个红色实心正方形 CGContextSetRGBFillColor(上下文, 255, 0, 0, 1); CGContextFillRect(上下文, CGRectMake(10, 10, 50, 50)); } - (void)viewDidLoad { [超级viewDidLoad]; self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; self.view.backgroundColor = [UIColor 灰色颜色]; UILabel *labelA = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 20)]autorelease]; labelA.text = @"mk ss9"; [self.view addSubview:labelA]; }
标签显示正常,但线条和方块没有显示,缺少什么?
多谢!
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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,明白了!
问题是我试图在我的 ViewController 中绘制,但这不是方法,
所以我创建了一个新类,其中包含 UIView 子类,现在它工作正常!
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!