如何在Swift中的两个视图之间绘制线

发布于 01-24 00:21 字数 255 浏览 3 评论 0原文

我想从一个对象绘制线到另一个对象。就像匹配的对象游戏一样。我想要使​​用Uikit使用Swift语言。在这里,我附上示例屏幕截图。

有人有经验吗?那请帮助我。

I want to Draw lines from one object to another object. Like a Matching object game. I want Swift language with UIKit Use. Here I attach the Sample Screenshot.

enter image description here

Does anyone have experience with this? then please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酷遇一生 2025-01-31 00:21:51

您有几个选择。

您可以创建一个Uiview的自定义子类,该类别实现draw(_:)函数来绘制自定义内容。该视图需要足够大才能包含所有自定义图纸。您将用清晰的视图填充大部分视图,然后使用核心图形绘制想要的线。

另一个选项是使用核心动画层。您可以在视图的层中添加CashApelayer,设置具有所需的线厚度和颜色的形状层,然后在包含要绘制的线的图层中添加路径。 (请注意,如果您使用CashApelayers,则以单个形状层绘制的所有线条都是相同的颜色。您需要多种形状层才能绘制多种颜色。)

两种方法都需要进行一些研究。形状层更有效,可以更好地利用iOS设备上的图形硬件,但是使用它们的学习曲线相当陡峭。

您应该能够使用两种方法的Google示例。尝试搜索短语,例如“ Uiview中的自定义绘图”和“带有CashApelayer的绘图”。我写了一个名为

编辑:

我将代码从示例应用程序调整为一个名为LinesBetweews的演示。新的演示具有自定义Uiview,可以在您放入的所有子视图对之间绘制线条。

这是演示:

编辑#2

请参阅此github repo 用于在子视图对之间绘制线条的示例项目。

You have a couple of choices.

You can create a custom subclass of UIView that implements the draw(_:) function to draw custom content. That view would need to big enough to enclose all of your custom drawing. You'd fill most of the view with clear, and then draw the lines you want using Core Graphics.

The other option is to use Core Animation layers. You could add a CAShapeLayer to your view's layer, set up the shape layer with the desired line thickness and color, and add a path to the layer containing the lines you want to draw. (Note that if you use CAShapeLayers, all the lines drawn in a single shape layer will be the same color. You'll need multiple shape layers in order to draw in multiple colors.)

Both approaches will require some research. Shape layers are more efficient and take better advantage of the graphics hardware on iOS devices, but using them has a fairly steep learning curve.

You should be able to google examples of both approaches. Try search phrases like "Custom drawing in a UIView" and "drawing with CAShapeLayer". I wrote a little demo app called OvalView that demonstrates how to create a UIView subclass that manages a shape layer.

Edit:

I adapted the code from my sample app into a demo called LinesBetweenViews. The new demo has a custom UIView that draws lines between any pairs of subviews you put into it.

Here is a screenshot of the demo:

enter image description here

If you answer my questions I can upload the demo app to Github so you can look at it.

Edit #2

See this Github repo for a sample project that draws lines between pairs of subviews.

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