Search
Duplicate

Lottie 애니메이션 사용하기

생성일
2023/08/09 10:11
태그
Swift

Lottie 애니메이션 사용하기

1. CocoaPods으로 import 하기

1.
프로젝트를 닫고 터미널을 열어 프로젝트의 루트 폴더로 이동한다.
2.
프로젝트의 루트 폴더에 Podfile 파일을 수정한다. (Xcode로 수정!)
# Uncomment the next line to define a global platform for your project # platform :ios, '13.0' target 'YourProject' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for CleanKinder pod 'lottie-ios' end
Ruby
복사
3.
파일을 저장하고 터미널의 루트 폴더에서, 다음 명령어를 입력한다.
pod install
Swift
복사
4.
코드 작성하기
import UIKit import Lottie class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let animationView: LottieAnimationView = .init(name: "homeKid") self.view.addSubview(animationView) animationView.frame = self.view.bounds animationView.center = self.view.center animationView.contentMode = .scaleAspectFit animationView.play() } }
Swift
복사
5.
결과물

UIView에 로티 애니메이션 넣어서 처리하기

애니메이션 루프 설정

// 애니메이션을 1번만 실행함 animationView.loopMode = .playOnce // 애니메이션을 3번만큼 실행하고 종료 animationView.loopMode = .repeat(3) // 애니메이션을 무한으로 실행 animationView.loopMode = .loop // 애니메이션을 실행한 뒤, 실행한 애니메이션을 거꾸로 다시 실행함 (3번만큼 실행) animationView.loopMode = .repeatBackwards(1) // 애니메이션을 실행한 뒤, 실행한 애니메이션을 거꾸로 다시 실행함 (무한 반복) animationView.loopMode = .autoReverse
Swift
복사

애니메이션 속도 조정

// 애니메이션 0.5배속 재생 animationView.animationSpeed = 0.5 // 애니메이션 2배속 재생 animationView.animationSpeed = 2
Swift
복사

2. Swift Package Manager로 import 하기