如何在 iPhone 上画线?

发布于 2024-07-19 10:42:19 字数 78 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2024-07-26 10:42:19

第一步是定义 UIView 的子类,以创建一个绘图空间。

如果您要开始一个新的应用程序,最简单的方法是从“基于窗口的应用程序”模板开始。

然后转到“新建文件”并创建一个“Objective-C 类”,将“子类”设置为“UIView”,并为其命名,例如 MyView.m。

现在打开“Resources”组并双击“MainWindow.xib”以在 Interface Builder 中将其打开。 从这里您应该看到一个名为“Window”的窗口。 按 Cmd+Shift+L 打开库,然后将“视图”组件拖到窗口上,并将其放置在可以看到全部内容的位置。 选择新视图后,按 Cmd+4 打开身份检查器,然后在“类身份”下,单击下拉列表并选择 MyView。

接下来,您需要在 MyView.m 中实现 drawRect: 方法,下面是一些绘制线条的示例代码:

- (void)drawRect:(CGRect)rect {
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 5.0f, 5.0f);
    CGContextAddLineToPoint(c, 50.0f, 50.0f);
    CGContextStrokePath(c);
}

保存所有内容并单击“构建并运行”,您现在应该在 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:

- (void)drawRect:(CGRect)rect {
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 5.0f, 5.0f);
    CGContextAddLineToPoint(c, 50.0f, 50.0f);
    CGContextStrokePath(c);
}

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".

帝王念 2024-07-26 10:42:19

您还可以使用 UIBezierPath 绘制一条线。 下面将绘制一条垂直居中的水平线:

- (void)drawRect:(CGRect)rect {
    CGFloat rectHeight = CGRectGetHeight(rect);
    CGFloat rectWidth = CGRectGetWidth(rect);

    UIBezierPath *line = [UIBezierPath bezierPath];
    [line moveToPoint:CGPointMake(0, rectHeight / 2)];
    [line addLineToPoint:CGPointMake(rectWidth, rectHeight / 2)];

    [[UIColor lightGrayColor] setStroke];
    [line stroke];
}

You can also draw a line using UIBezierPath. The following will draw a vertically-centered horizontal line:

- (void)drawRect:(CGRect)rect {
    CGFloat rectHeight = CGRectGetHeight(rect);
    CGFloat rectWidth = CGRectGetWidth(rect);

    UIBezierPath *line = [UIBezierPath bezierPath];
    [line moveToPoint:CGPointMake(0, rectHeight / 2)];
    [line addLineToPoint:CGPointMake(rectWidth, rectHeight / 2)];

    [[UIColor lightGrayColor] setStroke];
    [line stroke];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文