如何在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.
Does anyone have experience with this? then please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有几个选择。
您可以创建一个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:
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.