2017-05-12 9 views
1

touchesBeganとtouchesMoved点から作成したUIBezierPath配列があります。 私はそれらの描画を画面にアニメーション化したいと思います。UIBezierPath配列のアニメーション図面

ベゼルを繰り返し実行してsetNeedsDisplay()を呼び出さなければ、最良の方法はありますか?

おかげ

override func draw(_ rect: CGRect) { 
     for bezier in beziers{ 
     bezier.stroke() 
     } 
    } 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     super.touchesBegan(touches, with: event) 
     let cgPoint = touches.first!.location(in: self) 
     let bezierPath = ColorBezierPath() // subclass of UIBezierPath 
     bezierPath.lineWidth = lineWidth 
     bezierPath.color = color 
     bezierPath.move(to: cgPoint) 
     beziers.append(bezierPath) 
    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     super.touchesMoved(touches, with: event) 
     let cgPoint = touches.first!.location(in: self) 
     let bezierPath = beziers.last 
     bezierPath?.addLine(to: cgPoint) 
     setNeedsDisplay() 
    } 
+0

要件は描画アプリケーションに関連していますか? –

+0

@PraveenKumarはい、 "Draw Something"に似ています。UIBezierPath配列を保存して後でアニメーションの描画を描画したいのですか? – Maor

+0

後で何を意味するのですか?何かを描画するたびにユーザーを見せたくないですか? ! –

答えて

2

はあなたの最善の策はCAShapeLayerを使用することです。パスとその他のさまざまな設定が必要です。アニメーションを作成するには、strokeEndプロパティのアニメーションを0.0から1.0に変更します。これにより、必要なアニメーションタイミングパラメータを指定して、パスを最初から最後まで描画する効果が得られます。

let shapeLayer = CAShapeLayer()    // strokeEnd's model value is 1.0 by default 
shapeLayer.frame = containerLayer.bounds 
shapeLayer.path = bezierPath.cgPath 
containerLayer.addSublayer(shapeLayer) 

let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd") 
strokeEndAnimation.duration = 2.0 
strokeEndAnimation.fromValue = 0.0 

shapeLayer.add(strokeEndAnimation, forKey: "strokeEndAnimation") 
+0

ありがとう!何らかの理由で 'shapeLayer.fillColor'がデフォルトで' .black'に設定されています。奇妙な効果をもたらした。わかるにはしばらく時間がかかりました – Maor