LOTTIE Animation Use 3 Json File On Boarding Swift 4




How to Use more than one json file in library animation LOTTIE.

this example for 3 json file animation.

Note: use UICollectionview & UICollectionViewCell

import UIKit
import Lottie

class LoViewController: UIViewController {
    @IBOutlet weak var viewColCell: UIView!
    @IBOutlet weak var pageCon: UIPageControl!
    @IBOutlet weak var collView: UICollectionView!
    let animationIntro1 = LOTAnimationView(name: "intro-1") //json 1
    let animationIntro2 = LOTAnimationView(name: "intro-2") //json 2
    let animationIntro3 = LOTAnimationView(name: "intro-3") //json 3
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setConponent()
        collView.backgroundColor = UIColor.clear
        
        //animation 1
        animationIntro1.frame = CGRect(x: 0, y: 50, width: self.viewColCell.frame.size.width, height: 300)
        animationIntro1.contentMode = .scaleAspectFill
        animationIntro1.loopAnimation = false
        
        //animation 2
        animationIntro2.frame = CGRect(x: 0, y: 50, width: self.viewColCell.frame.size.width, height: 300)
        animationIntro2.contentMode = .scaleAspectFill
        animationIntro2.loopAnimation = false
        
        //animation 3
        animationIntro3.frame = CGRect(x: 0, y: 50, width: self.viewColCell.frame.size.width, height: 300)
        animationIntro3.contentMode = .scaleAspectFill
        animationIntro3.loopAnimation = false
        
        self.viewColCell.addSubview(animationIntro1)
        self.viewColCell.addSubview(animationIntro2)
        self.viewColCell.addSubview(animationIntro3)
        
        animationIntro1.play()
        animationIntro2.isHidden = true
        animationIntro3.isHidden = true
        
    }
    
    func setConponent() {
        self.collView.dataSource = self
        self.collView.delegate = self
        self.collView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell1")
        
        
        self.pageCon.hidesForSinglePage = true
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        self.pageCon.currentPage = indexPath.section
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.width
        pageCon.currentPage = Int(page)
        
        
        if page == 0 {
            animationIntro1.play()
            animationIntro1.isHidden = false
            animationIntro2.isHidden = true
            animationIntro3.isHidden = true
            
        } else if page == 1 {
            animationIntro2.play()
            animationIntro1.isHidden = true
            animationIntro2.isHidden = false
            animationIntro3.isHidden = true
            
        } else if page == 2 {
            animationIntro3.play()
            animationIntro1.isHidden = true
            animationIntro2.isHidden = true
            animationIntro3.isHidden = false
        }
    }
    
}

link LOTTIE github




No comments: