2016-10-11 43 views
0

我需要幫助我有這段代碼這是做循環路徑,但我需要的是直線我試圖做到這一點,但不成功。swift中的直線路徑

override func viewDidAppear(animated: Bool) { 



    super.viewDidAppear(animated) 

    let orbit = CAKeyframeAnimation(keyPath: "position") 
    var affineTransform = CGAffineTransformMakeRotation(0.0) 
    affineTransform = CGAffineTransformRotate(affineTransform, CGFloat(M_PI)) 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 150 - (100/2),y: 150 - (100/2)), radius: CGFloat(150), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 

    orbit.circlePath = path.CGPath 
    orbit.duration = 8 
    orbit.additive = true 
    orbit.repeatCount = 0.25 
    orbit.calculationMode = kCAAnimationPaced 
    orbit.rotationMode = kCAAnimationRotateAuto 

    moveobj.layer .addAnimation(orbit, forKey: "orbit") 
} 

和更多的一個問題如何做到這一點後消失完成移動。

謝謝

回答

0

您可以使用CAShapeLayer輕鬆做到這一點。

let line = CAShapeLayer() 
    let linePath = UIBezierPath() 
    linePath.move(to: CGPoint(x: 100, y: 100)) 
    linePath.addLine(to: CGPoint(x: 300, y: 300)) 
    line.path = linePath.cgPath 
    line.strokeColor = UIColor.red.cgColor 
    self.view.layer.addSublayer(line) 

首先,創建一個CAShapeLayer。 其次,創建一個UIBezierPath來定義你的線路的路徑。第三,將bezier路徑移動到起始點。第四,添加貝塞爾路徑的終點。第五步,將bezier路徑應用於CAShapeLayer。 第六,應用筆觸顏色,以便在將其添加爲子圖層時可以看到該線條。最後,添加它是視圖的子圖層。

+0

我把這個放在哪裏?我不想畫直線我想直線移動對象可以喲幫助我請。謝謝 – dinis

+0

已經解決了謝謝 – dinis