Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift 3中的可拖动UIView之间创建连接线?然后测量线的角度_Ios_Swift - Fatal编程技术网

Ios 如何在Swift 3中的可拖动UIView之间创建连接线?然后测量线的角度

Ios 如何在Swift 3中的可拖动UIView之间创建连接线?然后测量线的角度,ios,swift,Ios,Swift,我需要在屏幕上有两个视图进行拖动(通过UIPangestureRecognitiser)和一条连接线,这样当我自由移动视图时,该线保持连接(就像视图之间的绳子一样)。测量这条线的角度。 这是到目前为止我的代码 var rect1:UIView! var rect2:UIView! override func viewDidLoad() { super.viewDidLoad() // create my two views

我需要在屏幕上有两个视图进行拖动(通过
UIPangestureRecognitiser
)和一条连接线,这样当我自由移动视图时,该线保持连接(就像视图之间的绳子一样)。测量这条线的角度。 这是到目前为止我的代码

    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 between 

    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,将创建数百行。
而且不知道下一步要做什么,这是因为当您反复调用
addLine
函数时,并没有删除上一行

对于remove previous line,在类的顶部声明
var line=CAShapeLayer()
,这样您就可以得到您绘制的上一行,然后无论何时调用
addLine
函数,只要在开始时从superview中删除上一行即可,如下所示:

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)
    }
}
您的结果将是:

效果很好,非常感谢。最后一步,我需要移动3个对象作为一个孔,这样我就可以对齐照片上的线并测量角度(例如三角形)。我尝试移动主视图,但屏幕开始抖动!如果你能帮忙的话!又是坦斯克!!请用您尝试过的代码提出不同的问题。谢谢