将UIView子类化为在Quartz中绘制 [英] Subclassing UIView to draw in Quartz
问题描述
我正在尝试使用Quartz 2D在我的iPhone应用程序中绘制一个图形。我知道我必须通过继承UIView并覆盖drawRect函数来完成这项工作。我正在尝试按照这个教程来执行此操作,但我无法理解工作。
I'm trying to draw a figure in my iPhone application using Quartz 2D. I understand that I have to do this by subclassing UIView and overriding the drawRect function. I'm trying to follow this tutorial to do this but I can't get it to work.
似乎子类化UIView没有创建像教程似乎暗示的.xib文件。子类化UIViewController(我知道这是错误的,但无论如何我都尝试过)确实创建了.xib文件,但似乎没有drawRect函数。
It seems subclassing UIView does not create a .xib-file like the tutorial seem to imply. Subclassing UIViewController (I know this is wrong but I tried it anyway) does create a .xib-file but does not seem to have the drawRect-function.
有没有人有这方面的经验,或者知道我做错了什么?
Has anyone any experience in this, or knows what I'm doing wrong?
提前谢谢你,
Tobias Tovedal
Thank you in advance, Tobias Tovedal
推荐答案
-
创建一个
UIView
子类并将您的绘图代码放入其drawRect:
方法。您不需要NIB文件(如果您自己绘制视图,为什么会这样?)。
Create a
UIView
subclass and put your drawing code in itsdrawRect:
method. You don't need a NIB file (why would you if you draw the view yourself?).
在视图控制器中,创建自定义的实例查看类,设置视图的框架并将其添加到视图控制器的视图中: [self.view addSubview:myCustomView];
。
In a view controller, create an instance of your custom view class, set the view's frame and add it to the view controller's view with: [self.view addSubview:myCustomView];
.
没有第3步。
这篇关于将UIView子类化为在Quartz中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!