iOS 응용 (swift)
[iOS - swift] JGProgressHUD 프레임워크 (iOS 로딩 뷰, loader, loading)
jake-kim
2020. 11. 26. 22:46

해당 프레임 워크가 좋은 이유
- 비동기 처리할 때 다른 로딩 뷰에 비해서 오류 x
- 로딩 뷰가 등장하면 뒤에있던 뷰들을 클릭하지 못함(안전상태)
- 자동으로 중앙에 배치
의존성
pod 'JGProgressHUD'
BaseViewController생성하여 여기에 로딩관련 로직 추가 ( 재사용성을 위함 )
//
// BaseViewController.swift
// Test
//
// Created by 김종권 on 2020/11/26.
//
import Foundation
import UIKit
import JGProgressHUD
class BaseViewController: UIViewController {
lazy var hud: JGProgressHUD = {
let loader = JGProgressHUD(style: .dark)
return loader
}()
func showLoading() {
DispatchQueue.main.async {
hud.show(in: self.view, animated: true)
}
}
func hideLoading() {
DispatchQueue.main.async {
hud.dismiss(animated: true)
}
}
}
각 ViewController는 BaseViewContorller를 상속받아서 위 함수 사용
//
// ViewController.swift
// Test
//
// Created by 김종권 on 2020/11/26.
//
import UIKit
import JGProgressHUD
class ViewController: BaseViewController {
@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func btnTap(_ sender: Any) {
showLoading()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.hideLoading()
}
}
}