2017-05-25 43 views
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,會創建數百行。 並不知道下一步該怎麼做 請幫忙!

回答

0

那是因爲當您一次又一次地調用addLine函數時,您並未刪除上一行。

對於去除前行頂部到類聲明var line = CAShapeLayer()這樣你可以得到前行你畫,然後當你調用addLine功能剛剛從上海華盈在像開始刪除前行:

line.removeFromSuperlayer() 

而且你完整的代碼將是:

import UIKit 

class ViewController: UIViewController { 

    var rect1:UIView! 
    var rect2:UIView! 
    var line = CAShapeLayer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     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) 

     addLine(start: rect1.center, toPoint:rect2.center) 
    } 

    func dragView(_ sender: UIPanGestureRecognizer) { 

     let point = sender.location(in: self.view) 
     let theDraggedView = sender.view! 
     theDraggedView.center = point 
     addLine(start: rect1.center, toPoint:rect2.center) 
    } 


    func addLine(start: CGPoint, toPoint end:CGPoint) { 

     line.removeFromSuperlayer() 
     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) 
    } 
} 

而你的結果將是: enter image description here

+0

非常感謝。最後一步,我需要將3個物體作爲孔移動,所以我可以將線對齊到一張照片上並測量一個角度(例如三角形)。我嘗試移動孔的主視圖,但屏幕開始晃動!如果你能幫助! Thansk再次! – MadNeuro

+0

請使用您嘗試過的代碼提出不同的問題。謝謝!! –

相關問題