iphone - 我可以在drawRect中绘制UIView实例吗?
我正在尝试如下
UIView * view = [UIView new];
view.frame = CGRectMake(50, 50, 200, 200);
view.bounds = CGRectMake(0, 0, 400, 400);
view.backgroundColor = [UIColor grayColor];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 300, 50)];
[btn setTitle:@"Button" forState:UIControlStateNormal];
[view addSubview:btn];
CALayer * layer = [view layer];
[layer drawInContext:ctx];
我的问题是
1)就像我们用CGContextRef绘制图像或文本一样,我们可以用同样的方式绘制UIView实例吗?
我对核心图形框架不太熟悉,所以请让我知道可能性。我的基本目标是能够通过任何方式使用 CGContextRef 在 drawRect 中绘制自定义视图。
先感谢您 !!!
I am trying something like below
UIView * view = [UIView new];
view.frame = CGRectMake(50, 50, 200, 200);
view.bounds = CGRectMake(0, 0, 400, 400);
view.backgroundColor = [UIColor grayColor];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 300, 50)];
[btn setTitle:@"Button" forState:UIControlStateNormal];
[view addSubview:btn];
CALayer * layer = [view layer];
[layer drawInContext:ctx];
My questions is
1) Like we draw images or text with CGContextRef , can we draw a UIView instance the same way ?
I am not so familliar with the core graphics framework , so please let me know the possibilities . My basic aim is to be able to draw custom views in drawRect using CGContextRef by any means .
Thank you in advance !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在
UIView
的子类中实现-(void)drawRect:(CGRect)rect
方法。在此方法中,您应该绘制到当前上下文。You have to implement
-(void)drawRect:(CGRect)rect
method in your subclass ofUIView
. In this method you should draw to the current context.