2017-10-10 56 views
0

我在CAShapeLayer中使用CAAnimation來簡單地爲從左到右描邊的線條(它只是一個更復雜的程序的簡化版本)設置動畫。我放慢了速度,以顯示第一季度幾乎是瞬間的,然後其餘的正常動畫。如何在CAAnimation中使用UIViewAnimationCurve選項

我看到,可以使用UIView.animate的選項來設置UIViewAnimationCurve選項(例如,.EaseIn或.Linear)。在CAShapeLayer中嵌入CAAnimation時可以設置這些選項嗎?是否有必要在每次運行它時創建動畫,或者是否存在「運行動畫」命令?

下面是我截下來代碼:

// the containing UIView is called mainView 

    mainView.layer.sublayers = nil 

    // create a line using a UIBezierPath 

    let path = UIBezierPath() 
    path.move(to: CGPoint(x: 100, y: 100)) 
    path.addLine(to: CGPoint(x: 300, y: 100)) 

    // create a CAShapeLayer 

    let shapeLayer = CAShapeLayer() 
    shapeLayer.frame = CGRect(x: 0, y: 0, width: 1024, height: 1024) 
    shapeLayer.strokeColor = UIColor.green.cgColor 
    shapeLayer.lineWidth = 100 
    shapeLayer.path = path.cgPath 

    // create the animation 

    mainView.layer.addSublayer(shapeLayer) 
    let animation = CABasicAnimation(keyPath: "LetterStroke") 

    animation.fromValue = 0 
    animation.toValue = 1 
    animation.duration = 10 

    shapeLayer.add(animation, forKey: "animation") 

提前非常感謝。

回答

0

您應該使用CABasicAnimationtimingFunction財產。有一個允許像

animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn) 
+0

這是正確的答案,非常感謝。實際上,我發現問題存在於其他地方,正如你可以在下面看到的,但事實上,你確實按照陳述回答了我的問題。 –

0

的問題是該行動畫出現在啓動的方式四分之一these規定的定時功能的構造函數。我忘了把東西竟然是在問題的代碼行我的問題。

shapeLayer.lineCap = kCALineCapRound

這與100線寬相結合,取得了線開始的從一開始的方式大致四分之一。我使lineWidth更小,並且它工作正常。

相關問題