【Xcode/Swift】LTMorphingLabelというライブラリを使ってみる

今回はラベルの文字をおしゃれに表示させることが出来るLTMorphingLabelというライブラリを紹介します。

必要な準備

Storyboard

ライブラリのインストール

Terminal
pod 'LTMorphingLabel'

コードの記述

import UIKit
import LTMorphingLabel

class ViewController: UIViewController {
    
    @IBOutlet private weak var label: LTMorphingLabel!
    
    //表示制御用タイマー
    private var timer: Timer?
    //String配列のindex用
    private var index: Int = 0
    //表示するString配列
    private let textList = ["Let's", "Make", "History", "Today"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupAnimationStyle()
    }
    
    //アニメーションのタイプを指定
    private func setupAnimationStyle() {
        label.morphingEffect = .scale
        //        label.morphingEffect = .scale
        //        label.morphingEffect = .evaporate
        //        label.morphingEffect = .fall
        //        label.morphingEffect = .pixelate
        //        label.morphingEffect = .sparkle
        //        label.morphingEffect = .burn
        //        label.morphingEffect = .anvil
    }
    
    @objc func update(timer: Timer) {
        //Text表示の更新を行う
        label.text = textList[index]
        index += 1
        if index >= textList.count {
            index = 0
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //タイマーの追加 (1.5秒ごとに表示テキストを変更する)
        timer = Timer.scheduledTimer(timeInterval: 1.5,
                                     target: self,
                                     selector: #selector(update(timer:)), userInfo: nil,
                                     repeats: true)
        timer?.fire()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        timer?.invalidate()
    }
    
}

ビルドをして確認

アニメーションのスタイルについては、個人的にscaleかevaporateがいい感じかなと思いました。

まとめ

今回は以上です、色んなライブラリを触ってみると面白い発見が出来ますね!

◎今日の格言

ー John F. Kennedy

Life is never easy. There is work to be done and obligations to be met – obligations to truth, to justice, and to liberty.

(人生とは決して簡単なものではない、為すべき仕事と果たすべき責務がある。それは【真実】、【正義】、そして【自由】への責務だ。

この記事は役に立ちましたか?

はい
いいえ
貴重なフィードバックありがとうございます!