LoginSignup
5
4

More than 5 years have passed since last update.

CABasicAnimation利用時にUIViewアニメーションのBeginFromCurrentStateと同じ効果を得る

Last updated at Posted at 2014-11-02
let animatedLayer = CAShapeLayer()
// animatedLayerの設定

let anim = CABasicAnimation(keyPath: "path")
anim.fromValue = (animatedLayer.presentationLayer() as? CAShapeLayer)?.path
anim.toValue = toPath
anim.duration = 0.25
anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
anim.removedOnCompletion = false
anim.fillMode = kCAFillModeForwards
animatedLayer.addAnimation(anim, forKey: "MyPathAnimation")

補足

-addAnimation:forKey:でkeyをnilにするかどうかでfromValueを設定するかが決まる。

  • keyをnilにする場合は、fromValueは設定しない
  • keyに何か設定する場合は、presentationLayerを利用してfromValueを設定する

これに加えて、animatedLayerに追加したanimとその状態を残すようにする。

また、animatedLayerはアニメーション終了後、非表示にすることを想定しているため、実際にレイヤーの値の更新(animatedLayer.path = toPath)は行っていない。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4