ios - 在 UIView 上绘制点和线

标签 ios swift drawing core-graphics

根据我在 Apple Swift 上找到的示例,我给自己做了一个学习 Swift 的练习 website :

enter image description here

如您所见,中间有一条河流和一些点,形成了一条路径。所以我开始在互联网上寻找类似的河流图像,并创建了一个 Xcode Playground 。这就是我现在拥有的:

enter image description here

所以基本上我有一个 UIView,它的 subview 包含我找到的河流图像和一个用 UIBezierPath 制作的点。

我的第一个问题是:这是在 UIView 上绘制的正确方法吗?我的意思是使用 UIBezierPath。我的第二个问题是:如何在UIView 中的精确坐标处绘制圆点? (UIBezierPath 还是其他?)

更准确地说,我的目的是制定一种算法,让程序识别图像,并根据像素颜色,从河流的起点到终点画一 strip 点的线,经过它之间中间。

最佳答案

要在 UIView 上绘制 UIBezierPath,请执行以下操作:

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)

此代码将在您的 View 上绘制一个半径为 8、坐标为 10,20 的点。

更新:Swift 4+

let xCoord = 10
let yCoord = 20
let radius = 8

let dotPath = UIBezierPath(ovalIn: CGRect(x: xCoord, y: yCoord, width: radius, height: radius))

let layer = CAShapeLayer()
layer.path = dotPath.cgPath
layer.strokeColor = UIColor.blue.cgColor

drawingView.layer.addSublayer(layer)

关于ios - 在 UIView 上绘制点和线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34206056/

相关文章:

ios - 在 IOS Google 登录期间隐藏 Webview 中的地址和状态栏

ios - 如何从 native iOS 应用程序重定向回使用深层链接/uni 链接启动我的应用程序的原始网页?

javascript - 二维数组javascript中的无限递归框

ios - swift - 从 Reddit API 解码 JSON 响应(发表评论)

cocoa - 有没有办法让 NSView 重绘更频繁?

Android:使用形状绘制 "X"

IOS swift TableView 有没有办法重用它们

ios - Graph API 未返回照片中所有标记的内容

ios - Swift -'init(cgImage:options:alphaType:)' 已弃用 : Replaced by MTIImage(cgImage:options:isOpaque:)

swift - 如何在取消选择 UITableView 中的行时从数组中删除项目