swiftで線分を以下のコードで表示させて、ドラッグした位置へ移動させようとしているのですが、移動前の線分が消えずに残ってしまいます。
円も同時に描写して、ドラッグで移動させようとすると、移動前の円は残らなかったです
どうかご教授ください
swift
1//線分 2 horizonalLine.strokeColor = UIColor.black.cgColor 3 horizonalLine.lineWidth = 3.0 4 line.move(to: CGPoint(x:sx,y:sy)) //始点 5 line.addLine(to:CGPoint(x:gx,y:gy)) //終点 6 line.close() //線を結ぶ 7 horizonalLine.path = line.cgPath 8 self.view.layer.addSublayer(horizonalLine) 9 10//円 11ovalShapeLayer.strokeColor = UIColor.blue.cgColor // 輪郭は青 12ovalShapeLayer.fillColor = UIColor.blue.cgColor // 塗りはクリア 13ovalShapeLayer.lineWidth = 1.0 14ovalShapeLayer.path = UIBezierPath(ovalIn: CGRect(x:sx, y:sy, width:5, height:5)).cgPath 15self.view.layer.addSublayer(ovalShapeLayer)
swift
1switch pointXY { 2 case (sx-20...sx+20,sy-20...sy+20): 3 // ドラッグ後の座標 4 let newDx = touchEvent.location(in: self.view).x 5 let newDy = touchEvent.location(in: self.view).y 6 7 // ドラッグしたx座標の移動距離 8 let dx = newDx - preDx 9 //print("x:(dx)") 10 11 // ドラッグしたy座標の移動距離 12 let dy = newDy - preDy 13 //print("y:(dy)") 14 15 sx += dx 16 sy += dy 17 18 //線 19 horizonalLine.strokeColor = UIColor.black.cgColor 20 horizonalLine.lineWidth = 3.0 21 line.move(to: CGPoint(x:sx,y:sy)) //始点 22 line.addLine(to:CGPoint(x:gx,y:gy)) //終点 23 line.close() //線を結ぶ 24 horizonalLine.path = line.cgPath 25 self.view.layer.addSublayer(horizonalLine) 26 27 //円 28 ovalShapeLayer.path = UIBezierPath(ovalIn: CGRect(x:sx, y:sy, width:5, height:5)).cgPath 29 self.view.layer.addSublayer(ovalShapeLayer) 30 31 ovalShapeLayer2.path = UIBezierPath(ovalIn: CGRect(x:gx, y:gy, width:5, height:5)).cgPath 32 self.view.layer.addSublayer(ovalShapeLayer2)
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/27 01:39
2018/12/27 01:44