0
グリッド内の2点ごとにそれらの点をタップして接続できるようにする必要があります。私はポイントのグリッドを作成するために管理:グリッド内の点と線の接続
func drawPointGrid() {
let points: CGFloat = 5
let cellWidth = bounds.width/points
let cellHeight = bounds.height/points
for i in 0..<Int(points) {
for j in 0..<Int(points) {
let circleX: CGFloat = ((CGFloat(i) + 0.5) * cellWidth)
let circleY: CGFloat = ((CGFloat(j) + 0.5) * cellHeight)
let centerCirclePath = UIBezierPath(ovalIn: CGRect(x: circleX, y: circleY, width: diameter, height: diameter))
let customlayer = CAShapeLayer()
customlayer.path = centerCirclePath.cgPath
customlayer.fillColor = UIColor.black.cgColor
layer.addSublayer(customlayer)
}
}
}
これは私の視点グリッドです:私は、ビュー上のラインを作るために管理
が、唯一の私は、開始点のためにクリックして、エンドポイントのためにもう一度クリックすると、 、行が作成されるので、私はこの行がそれらの間の水平方向と垂直方向のときに、ユーザータップ上のすべての2点間に作成する必要があります:私はCGPoint
を見つけfindEndPoints
と呼ばれる関数を書いた
override func draw(_ rect: CGRect) {
drawPointGrid()
tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showMoreActions))
tapGestureRecognizer.numberOfTapsRequired = 1
addGestureRecognizer(tapGestureRecognizer)
}
// draw line from point to point that are clicked
var firstPoint: CGPoint?
var secondPoint: CGPoint?
func showMoreActions(touch: UITapGestureRecognizer) {
let touchPoint = touch.location(in: self)
guard let _ = firstPoint else {
firstPoint = touchPoint
return
}
guard let _ = secondPoint else {
secondPoint = touchPoint
addLine(start: firstPoint!,end: secondPoint!)
firstPoint = nil
secondPoint = nil
return
}
}
func addLine(start: CGPoint,end:CGPoint) {
let line = CAShapeLayer()
let linePath = UIBezierPath()
linePath.move(to: start)
linePath.addLine(to: end)
line.path = linePath.cgPath
line.strokeColor = UIColor.black.cgColor
line.lineWidth = 2
line.lineJoin = kCALineJoinRound
layer.addSublayer(line)
}
それは、Uに感謝動作します。) – RosS