iPhone UIView 大小调整和图像绘制
我正在开发一个自定义 UIView 组件。这是一个简单的小秒表,所有数字均采用 PNG 图形。周围有很多教程,所以我自己解决了大部分问题。但我有两个问题:
首先,组件始终具有特定的大小(141 x 32),但我不知道如何从 UIView 子类中指定它。 “frame”属性不仅要定义宽度和高度,还要定义 x 和 y 位置。当然,在将组件放置到某个父视图中之前我不会知道其位置。我该怎么做?
其次,为了绘制组件内的所有数字图像,我不知道是否应该创建 UIImageView 对象并将它们布局为子对象,或者是否应该直接在视图的 drawRect 方法中绘制图像。秒表显示时:分:秒.第十个数据,因此每秒至少会更新一张图像十次,每秒更新一张图像。我可以轻松地重新分配 UIImageView 的“图像”属性,或者我可以直接使用 UIImages 重新绘制所有内容。
在决定使用哪种绘图策略时,我应该考虑哪些因素?在某些情况下,是否有一种选择比另一种更好?
编辑:
我还应该提到,我正在尝试完全不使用 XIB 文件来开发该组件,因为我正在尝试更好地了解自定义组件开发的底层机制。
I'm developing a custom UIView component. It's a simple little stopwatch, with PNG graphics for all the digits. There are lots of tutorials around, so I'm figuring most of it out on my own. But I have two questions:
First of all, the component will always have a particular size (141 x 32), but I don't know how to specify that from within the UIView subclass. The "frame" property wants me to define not only the width and height, but also the x and y position. Of course, I won't know the position until the component is placed into some parent view. How do I do that?
Secondly, for drawing the all the digit images within the component, I don't know whether I should create UIImageView objects and lay them out as children, or whether I should just draw the images directly within the view's drawRect method. The stopwatch displays hour:minute:second.tenth data, so there will be at least one image updated ten times per second, and one image updated every second. I could easily reassign the "image" property of the UIImageView, or I could just redraw everything using the UIImages directly.
What considerations should I take into account when deciding which drawing strategy to use? Are there some cases where one choice is better than the other?
EDIT:
I should also mention that I'm trying to develop this component entirely without XIB files, since I'm trying to get a better understanding of the underlying mechanics of custom component development.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将 0,0 表示帧中的 x,y 位置即可。当您将其添加到视图时,原点将设置为实际位置。
Just put 0,0 for x,y position in the frame initially. The origin will be set to an actual position when you add it to your view.