[iPhone] CABasicAnimationを使たアニメーション

アニメーションは処理が重いのですがCABasicAnimationを使うと比較的スムーズな動きを出せます。こちらで画像のモザイクを試しましたが、それを動画にしてみましょう。


Xcode 13.4.1

CABasicAnimation

CALayer の CA は Core Animationのことでアニメーションの画像処理系の重い処理に適しています。またUIViewと被っているような気もするのですが、その差は
CALayer
‘An object that manages image-based content and allows you to perform animations on that content’

アニメーション的な処理はこちらが使いやすいということのようです

UIView
‘An object that manages the content for a rectangular area on the screen.’

描画する領域について色々定義しているということでしょうか

モザイク画からフェードインするような演出を作ります。
rasterizationScale をキーにして CABasicAnimation のインスタンスを生成
duration でアニメーション期間を設定して、その間の開始と終了時のキー値を入れます。
imgView.layer に追加する
コードは以下のようになります。

ViewController.swift


あとは画像を選んでAssets.xcassetsに入れます。
(例:sample@2x.jpg)

 

ButtonをStoryboardに置いてコードと紐付けします。

 

Swiftのいつの頃からか、QuartzCore.framewarkを入れなくてもできるようになりましたね

画像のちょっとしたアレンジができそうです。


関連ページ:

References:
CABasicAnimation — Core Animation | Apple Developer Documentation
UIImageView — UIKit | Apple Developer Documentation
UIImage — UIKit | Apple Developer Documentation