我只能检测到 c1 中包含的起点(touchBegan)和 c2 中包含的终点(touchEnded),但无法检测到 c3 中它们之间的路径,我该怎么办?
override func layoutSubviews() {
self.clipsToBounds = true
self.isMultipleTouchEnabled = false
self.contentMode = .scaleToFill
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
swiped = false
lastPoint = touch?.location(in: self)
firstPoint = lastPoint
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
swiped = true
let currentPoint = touch?.location(in: self)
lastPoint = currentPoint
linePoint = lastPoint
drawShapeLayer(from: lastPoint, to: currentPoint!)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swiped {
drawShapeLayer(from: lastPoint, to: lastPoint!)
}
if c1.contains(firstPoint) && c3.contains(linePoint) && c2.contains(lastPoint){
AlertView.instance.showAlert(title: "Hooray!", message: "You made it!", alertType : .success)
}
else {
AlertView.instance.showAlert(title: "Oops!", message: "You've almost got it.", alertType : .failure)
}
}
}
最佳答案
根据我上面的评论,我认为 touchesMoved
中存在逻辑错误,如果您尝试绘制三点线,则需要重新排序代码,然后检查每个点位于特定 View 区域内。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
swiped = true
currentPoint = touch.location(in: self)
drawShapeLayer(from: lastPoint, to: currentPoint)
lastPoint = currentPoint
linePoint = currentPoint
}
}
(还可以安全地解开 touch 来删除可选内容,并避免在 Touches.first 为 nil 的情况下发生崩溃)
关于swift - 如何检测cgRect中的cgPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616521/