[swift] Swift SkeletonView 알림 기능 구현

안녕하세요! 이번에는 Swift SkeletonView 라이브러리를 사용하여 알림 기능을 구현하는 방법에 대해 알아보겠습니다. SkeletonView는 앱의 로딩 시간 동안 빈 화면에 임시 스켈레톤 UI를 보여줄 수 있도록 도와주는 라이브러리입니다.

1. SkeletonView 라이브러리 설치

먼저, SkeletonView 라이브러리를 설치해야 합니다. Cocoapods를 사용하는 경우, Podfile에 다음과 같이 라이브러리를 추가합니다.

pod 'SkeletonView'

그리고 터미널에서 pod install 명령어를 실행하여 라이브러리를 설치합니다.

2. 알림 기능을 포함한 View 구성

SkeletonView를 사용하여 빈 화면에 임시 스켈레톤 UI를 보여주기 위해 알림 기능이 포함된 View를 구성해야 합니다. 예를 들어 알림 목록을 보여주는 UITableView의 경우, 다음과 같이 구성할 수 있습니다.

import UIKit
import SkeletonView

class NotificationViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    var notifications: [Notification] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Dummy 데이터 생성
        generateDummyData()
        
        applySkeletonView()
    }
    
    func generateDummyData() {
        // 알림 데이터 생성
        for i in 1...5 {
            let notification = Notification(id: i, text: "알림 메시지 \(i)")
            notifications.append(notification)
        }
    }
    
    func applySkeletonView() {
        tableView.showSkeleton(usingColor: .lightGray) // 스켈레톤 색상 설정
        tableView.showAnimatedSkeleton() // 스켈레톤 애니메이션 효과
    }
    
    func hideSkeletonView() {
        tableView.hideSkeleton() // 스켈레톤 숨기기
    }
}

extension NotificationViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notifications.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationCell", for: indexPath)
        let notification = notifications[indexPath.row]
        cell.textLabel?.text = notification.text
        
        return cell
    }
}

struct Notification {
    let id: Int
    let text: String
}

위의 코드에서는 NotificationViewController라는 이름의 ViewController를 만들고, UITableView를 사용하여 알림 목록을 보여줍니다. generateDummyData 함수를 통해 더미 데이터를 생성하고, applySkeletonView 함수를 통해 스켈레톤 UI를 적용합니다.

3. 스켈레톤 UI 숨기기

실제 데이터를 불러와 알림 목록을 채우고 나면, 스켈레톤 UI를 숨겨야 합니다. hideSkeletonView 함수를 호출하여 스켈레톤 UI를 숨길 수 있습니다.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    hideSkeletonView()
}

위의 코드는 ViewController가 화면에 나타났을 때 hideSkeletonView 함수를 호출하여 스켈레톤 UI를 숨깁니다.

마무리

이제 SkeletonView 라이브러리를 사용하여 알림 목록과 같은 UI 요소에 임시 스켈레톤 UI를 적용하는 방법을 알아보았습니다. SkeletonView를 사용하여 앱의 로딩 시간 동안 사용자들에게 빈 화면보다는 풍성한 UI를 보여줄 수 있습니다.