私は、画面に線を引くためにCAShapeLayerを使用します。メソッドでtouchesEnded私は "ラインがポイントを通過するかどうかチェックしますか?"私のコードでは、画面の任意の部分を押すとのメソッドが常にtrueを返します。おそらく、私は問題があるline.frame =(view?.bounds)!。どうすれば修正できますか? 私の悪い英語を申し訳ありません。CAShapeLayer。ラインはポイントを通過しますか?
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")
}
}
[線分と点](http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line)との距離を計算し、値がある値以下になるかどうかを確認することをお勧めします。 – Rob