簡體   English   中英

直線路徑迅速

[英]Straight line path in 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")
}

還有更多的問題是如何完成移動后消失的。

謝謝

您可以使用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路徑應用於CAShapeLayer。 第六,應用筆觸顏色,以便在將其添加為子圖層時可以看到該線。 最后,添加它是視圖的子層。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM