- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我使用 CAShapeLayer 来在屏幕上画一条线。在方法 touchesEnded 中,我想检查“线是否通过该点?”。在我的代码中,当我按下屏幕的任何部分时,方法 contains 始终返回 true。也许,我在 line.frame = (view?.bounds)! 有问题。我该如何解决? 抱歉我的英语不好。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let firstPosition = touch?.location(in: self)
if atPoint(firstPosition!) == lvl1 {
let firstPositionX = firstPosition?.x
let firstPositionY = frame.size.height - (firstPosition?.y)!
view?.layer.addSublayer(line)
line.lineWidth = 8
let color = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).cgColor
line.strokeColor = color
line.fillColor = nil
line.frame = (view?.bounds)!
path.move(to: CGPoint(x: firstPositionX!, y: firstPositionY))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let firstPosition = touch?.location(in: self)
if atPoint(firstPosition!) == lvl1 {
let firstPositionX = firstPosition?.x
let firstPositionY = frame.size.height - (firstPosition?.y)!
path.addLine(to: CGPoint(x: firstPositionX!, y: firstPositionY))
line.path = path.cgPath
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if line.contains(screenCenterPoint) {
print("ok")
}
}
最佳答案
方法func contains(_ p: CGPoint) -> Bool
的 CAShapeLayer
返回 true
如果层的 bounds 包含该点。 ( see documentation )
所以你不能用它来检查线是否包含一个点。
然而,在类 CGPath
中还有另一个同名方法。返回指定点是否在路径的内部。但是由于您只是描边您的路径而您没有填充内部,因此此方法也不会给出所需的结果。
诀窍是使用以下方法创建路径的轮廓:
let outline = path.cgPath.copy(strokingWithWidth: line.lineWidth, lineCap: .butt, lineJoin: .round, miterLimit: 0)
然后检查大纲内部是否包含你的screenCenterPoint
if outline.contains(screenCenterPoint) {
print("ok")
}
由于您仅在触及末端时才检查包含,我认为创建路径轮廓不会增加太多开销。
当您想实时检查包含情况时,例如在touchesMoved
内部函数,计算轮廓可能会产生一些开销,因为每秒调用此方法很多次。此外,路径越长,计算轮廓所需的时间就越长。
因此,最好只实时生成最后绘制的线段的轮廓,然后检查该轮廓是否包含您的观点。
如果你真的想减少开销,你可以编写自己的包含函数。直线上的点的包含非常简单,可以简化为以下公式:
Given a line from
start
toend
withwidth
and a pointp
Calculate:
dx = start.x - end.x
dy = start.y - end.y
a = dy * p.x - dx * p.y + end.x * start.y - end.y * start.x
b = hypot(dy, dx)
The line contains point
p
if:
abs(a/b) < width/2
and p is in the bounding box of the line.
关于ios - CAShapeLayer。直线穿过点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532330/
我在 UITextField 上遇到了这个奇怪的错误,有时有一条线(实际上更多的是透明条)穿过它。有谁知道怎么去掉吗? 这是图片 xib 文件的链接:http://db.tt/7l2Bq42 代码链接
这个问题在这里已经有了答案: ggplot2 and a Stacked Bar Chart with Negative Values (1 个回答) 7年前关闭。 friend 们, 如何在 x 轴
首先我是初学者,我想在向下滚动页面并穿过某个 div 时显示一个菜单栏(当我页面顶部的实际菜单栏不再可见时)这个网站有一个完美的例子来解决我的问题:http://lenssenmannenmode.n
我怎样才能阻止这种情况发生? 这是我联系我们表单的代码。我正在使用 Bootstrap 。当我展开文本区域时,它会穿过分区。我可以通过添加内联 CSS 在文本区域内尝试任何替代方法吗?
如何用matplotlib绘制这样的图? 最佳答案 检查 this example来自 this group 例如: from matplotlib import pyplot as plt impo
所以我一直在研究一款游戏,到目前为止最大的问题是我们无法让玩家与场上的任何物体发生碰撞。相反,他们直接穿过树。谁能告诉我为什么?这是我尝试用于碰撞检测的代码: for tree in treelist
我有这个代码: my link 1 my link 2 my link 3 当我对 应用填充时像这样: ul { list-style: none; margin: 3
我对 pinescript 很新,我被困在这一点上......我只想在 10 EMA 穿过 21 EMA 而 21 高于 50 EMA 和 50 EMA 高于 200 EMA 时绘制交叉。这是指示多头
我是一名优秀的程序员,十分优秀!