如何在 iPhone 上画线?
我是 iPhone 编程的初学者,想使用 Quartz 和 UIKit 在手机屏幕上画一条线以进行学习。
我如何开始绘画?
I am a beginner at iPhone programming, and would like to draw a line to the phone screen for the purpose of study using Quartz and UIKit.
How do I start drawing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一步是定义 UIView 的子类,以创建一个绘图空间。
如果您要开始一个新的应用程序,最简单的方法是从“基于窗口的应用程序”模板开始。
然后转到“新建文件”并创建一个“Objective-C 类”,将“子类”设置为“UIView”,并为其命名,例如 MyView.m。
现在打开“Resources”组并双击“MainWindow.xib”以在 Interface Builder 中将其打开。 从这里您应该看到一个名为“Window”的窗口。 按 Cmd+Shift+L 打开库,然后将“视图”组件拖到窗口上,并将其放置在可以看到全部内容的位置。 选择新视图后,按 Cmd+4 打开身份检查器,然后在“类身份”下,单击下拉列表并选择 MyView。
接下来,您需要在 MyView.m 中实现 drawRect: 方法,下面是一些绘制线条的示例代码:
保存所有内容并单击“构建并运行”,您现在应该在 iPhone 上看到一条短红线。
有关 Core Graphics 的更多信息,请查找 Apple 文档。 我还发现在 Xcode 文档查看器中搜索以 CGContext 开头的函数并浏览这些函数很有帮助 - 您最终使用的大多数 Core Graphics 函数将以术语“CGContext”开头。
The first step is to define a subclass of UIView, to create a space to draw in.
If you're starting with a new application, the easiest way will be to start with the "Window-based application" template.
Then go New File and create an "Objective-C Class" with "Subclass of" set to "UIView", and give it a name, say MyView.m.
Now open up the "Resources" group and double click on "MainWindow.xib" to open it in Interface Builder. From here you should see a window named "Window". Hit Cmd+Shift+L to bring up the Library, and drag a "View" component onto your window, and position it so you can see all of it. With your new View selected, hit Cmd+4 to bring up the Identity Inspector and under "Class Identity", click the dropdown and choose MyView.
Next, you need to implement the drawRect: method in MyView.m, here's some example code that draws a line:
Save everything and click "Build and Run", you should now see a short red line on the iPhone.
For more information about Core Graphics, look up the Apple Documentation. I've also found it helpful to search for functions beginning with CGContext in the Xcode documentation viewer, and browse through those - most of the Core Graphics functions you'll end up using will start with the term "CGContext".
您还可以使用 UIBezierPath 绘制一条线。 下面将绘制一条垂直居中的水平线:
You can also draw a line using
UIBezierPath
. The following will draw a vertically-centered horizontal line: