0
我需要在屏幕上有兩個視圖拖動(完成白色UIPanGestureRecogniser
)和之間的連接線,所以當我自由地移動視圖時,線保持連接(如視圖之間的繩索)。並測量這條線的角度。 這是我的代碼到目前爲止如何在swif 3中的可拖拽UIViews之間創建一條連線?並測量線的角度
var rect1:UIView!
var rect2:UIView!
override func viewDidLoad() {
super.viewDidLoad()
// create my two views
rect1 = UIView(frame: CGRect(x: 50, y: 150, width: 40, height: 40))
rect1.backgroundColor = UIColor.orange
self.view.addSubview(rect1)
rect2 = UIView(frame: CGRect(x: 200, y: 150, width: 40, height: 40))
rect2.backgroundColor = UIColor.yellow
self.view.addSubview(rect2)
// and the UIPanGestureRecognizer objects
let gesture1 = UIPanGestureRecognizer(target: self, action: #selector(dragView))
rect1.addGestureRecognizer(gesture1)
let gesture2 = UIPanGestureRecognizer(target: self, action: #selector(dragView))
rect2.addGestureRecognizer(gesture2)
// add mi connecting line betwen
func addLine(fromPoint start: rect1.center, toPoint end:rect2.center)
}
// create the func for moving the views
func dragView(_ sender: UIPanGestureRecognizer)
{
let point = sender.location(in: self.view)
let theDraggedView = sender.view!
theDraggedView.center = point
}
// and the func for line creation
func addLine(fromPoint start: CGPoint, toPoint end:CGPoint)
{
let line = CAShapeLayer()
let linePath = UIBezierPath()
linePath.move(to: start)
linePath.addLine(to: end)
line.path = linePath.cgPath
line.strokeColor = UIColor.red.cgColor
line.lineWidth = 2
line.lineJoin = kCALineJoinRound
self.view.layer.addSublayer(line)
}
}
這裏我是股票! 如果我在dragView中再次使用addline func,會創建數百行。 並不知道下一步該怎麼做 請幫忙!
非常感謝。最後一步,我需要將3個物體作爲孔移動,所以我可以將線對齊到一張照片上並測量一個角度(例如三角形)。我嘗試移動孔的主視圖,但屏幕開始晃動!如果你能幫助! Thansk再次! – MadNeuro
請使用您嘗試過的代碼提出不同的問題。謝謝!! –