之后更改开始和结束点calayer

时间:2016-09-12 17:48:19

标签: swift calayer

我这样画一条线:

func addDashedLine(fromPoint start: CGPoint, toPoint end:CGPoint) -> CAShapeLayer {
        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.moveToPoint(start)
        linePath.addLineToPoint(end)
        line.path = linePath.CGPath
        line.strokeColor = UIColor.redColor().CGColor
        line.lineWidth = 1
        line.lineJoin = kCALineJoinRound
        line.lineDashPattern = [4, 4]

        return line
    }

我以这种方式追加它:

 var linee : CAShapeLayer?

override func viewDidLoad() {
    super.viewDidLoad()

    var i = 0;

    // Using a for loop, create the labels to be drag-and-dropped
    for word in points {

        let label = UILabel()
        label.font = UIFont.systemFontOfSize(32)
        label.sizeToFit()
        label.backgroundColor = UIColor.orangeColor()
        label.tag = i;

        label.center = CGPoint(x: word.x, y: word.y)

        let panGesture = UIPanGestureRecognizer(target: self, action: Selector("handlePanGesture:"))

        label.addGestureRecognizer(panGesture)
        label.userInteractionEnabled = true

        view.addSubview(label)
        i += 1;
    }


    linee = self.addDashedLine(fromPoint: points[0], toPoint: points[3])
    cropView.layer.addSublayer(linee!)
}

好的,到目前为止没有问题。我连接标签坐标上的线。但是当我移动标签时,我想要更改为线的起点或终点。这适用于起点。但是我怎么能在我的终点做到这一点?

func handlePanGesture(panGesture: UIPanGestureRecognizer) {
        // get translation (the coordinate changes)
        var translation = panGesture.translationInView(view)
        // reset translation to 0 once coordinate changes registered
        panGesture.setTranslation(CGPointZero, inView: view)

        // add dx, dy to current label center position
        var label = panGesture.view as! UILabel


        if(label.tag == 1){
            CATransaction.begin()
            CATransaction.setValue(true, forKey: kCATransactionDisableActions)
            linee?.position = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)

            CATransaction.commit()

        }
        label.center = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)
    }

0 个答案:

没有答案