在UIView上绘制点和线 [英] Drawing dots and lines on to a UIView
问题描述
基于我在Apple Swift
正如您所看到的那样河流和中间的几个点,形成一条路。所以我开始在互联网上寻找类似的河流图像,并创建了一个Xcode游乐场。这就是我现在所拥有的:
< img src =https://i.stack.imgur.com/rHZUN.pngalt =在此处输入图像说明>
所以基本上我有一个 UIView
,其子视图包含我找到的河图,以及用 UIBezierPath
制作的点。
我的第一个问题是:这是吸引UIView的正确方法吗?我的意思是使用 UIBezierPath
。我的第二个问题是:如何在 UIView
内的精确坐标处绘制点? ( UIBezierPath
或其他一切?)
为了更精确,我的目的是制作算法使程序识别图像,并根据像素颜色,从河的开始到结束绘制一条点线,在它的中间穿过。
在 UIView <上绘制
UIBezierPath
/ code>执行此操作:
让xCoord = 10
让yCoord = 20
let radius = 8
let dotPath = UIBezierPath(ovalInRect:CGRectMake(xCoord,yCoord,radius,radius))
let layer = CAShapeLayer()
layer.path = dotPath.CGPath
layer.strokeColor = UIColor.blueColor()。CGColor
drawingView.layer.addSublayer(layer)
此代码将在您的视图上绘制一个半径为8且坐标为10,20的点。
I gave myself an exercise to learn Swift, based on an example I have found on the Apple Swift website:
As you can see there's a river and a few dots in it right in the middle, forming a path. So I have started looking for a similar river image on the internet and I have created a Xcode playground. This is what I have now:
So basically I have an UIView
with a subview consisting in the river image I have found and a dot made with UIBezierPath
.
My first question is: is this the right way to drawn on to a UIView? I mean using a UIBezierPath
. And my second question is: how do I draw the dot at a precise coordinate inside the UIView
? (UIBezierPath
or everything else?)
Just to be more precise, my intent here is to make an algorithm to make the program recognize the image and based on the pixel color it would draw a line with dots from the start to the end of the river, passing between it's middle.
To draw UIBezierPath
on UIView
do this:
let xCoord = 10
let yCoord = 20
let radius = 8
let dotPath = UIBezierPath(ovalInRect: CGRectMake(xCoord, yCoord, radius, radius))
let layer = CAShapeLayer()
layer.path = dotPath.CGPath
layer.strokeColor = UIColor.blueColor().CGColor
drawingView.layer.addSublayer(layer)
This code will draw a dot with radius 8 with coordinates 10,20 on your view.
这篇关于在UIView上绘制点和线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!