Swift HUD库PKHUD


介绍

我认为最著名的

HUD库是SVProgressHUD,但是这次我检查了Swift制作的PKHUD

为什么PKHUD

这是因为,由于在

GitHub上搜索HUD和Swift中的语言而导致的星数最高。 (截至2018年12月10日)

  • PKHUD
  • 迅捷通知
  • KRProgressHUD
  • 甚值

粗略比较

<表格>

库名

星数

Swift

Xcode

其他


<身体>

PKHUD

3057

4.2

Xcode 10


SwiftNotice

755

4.0

Xcode 9.3

安装方法仅为手动

KRProgressHUD

338

4.2

Xcode 10

提供日语文档

VHUD

138

4.2

Xcode 10




关于PKHUD

功能

  • 根据Apple的设计制造
  • 您可以显式指定要从中显示的视图
  • 高度可定制

如何使用

基本操作

提供了HUD类,使您可以轻松显示HUD。
这使得查看加载和状态非常容易。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// HUDを表示
HUD.show(.progress)
HUD.show(.progress, onView: view) // 表示もとのviewを明示的に指定

// HUDを表示して指定時間後に非表示にする
HUD.flash(.progress, delay: 3)
// HUDを出し終わったあとのタイミングが取れる
HUD.flash(.success, onView: view, delay: 2) { _ in
            // HUDを非表示にしたあとの処理
        }

// HUDを非表示にする
HUD.hide()
HUD.hide { _ in
            // HUDを非表示にしたあとの処理
        }

特定类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public enum HUDContentType {

    case success

    case error

    case progress

    case image(UIImage?)

    case rotatingImage(UIImage?)

    case labeledSuccess(title: String?, subtitle: String?)

    case labeledError(title: String?, subtitle: String?)

    case labeledProgress(title: String?, subtitle: String?)

    case labeledImage(image: UIImage?, title: String?, subtitle: String?)

    case labeledRotatingImage(image: UIImage?, title: String?, subtitle: String?)

    case label(String?)

    case systemActivity
}

示例)
成功
スクリーンショット 2018-12-10 22.21.06.png

错误
スクリーンショット 2018-12-10 22.18.49.png

进度
スクリーンショット 2018-12-10 22.29.08.png

背景
您可以设置是否使背景变暗。
默认情况下,它设置为变暗。

1
2
HUD.dimsBackground = false
HUD.show(.progress, onView: view)

<表格>

dimsBackground

true




<身体>

スクリーンショット 2018-12-11 16.56.27.png


关于自定义

HUD提供的一个效果很好,但是我认为有时需要自定义它。
在这种情况下,请使用PKHUD
这可以通过将自定义视图设置为PKHUD.sharedHUD.contentView来实现。
可以使用已经在PKHUD中定义的视图(PKHUDSquareBaseViewPKHUDWideBaseView)轻松地对其进行自定义。

示例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        PKHUD.sharedHUD.contentView = CustomHUDView(image: PKHUDAssets.checkmarkImage, title: "Success!", subtitle: nil)
        PKHUD.sharedHUD.show(onView: view)
    }
}

class CustomHUDView: PKHUDSquareBaseView {

    override init(image: UIImage?, title: String?, subtitle: String?) {
        super.init(image: image, title: title, subtitle: subtitle)

        titleLabel.textColor = UIColor.lightGray
        backgroundColor = UIColor(red: 0xAB/0xFF, green: 0xD2/0xFF, blue: 0xFC/0xFF, alpha: 1.0)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

スクリーンショット 2018-12-11 2.28.35.png

注释
我自定义了PKHUDSquareBaseView,但是如果要查看加载,请将"带有加载的视图"功能再次设置为PKHUD.sharedHUD.contentView
根据您要显示的内容,您每次都需要更改PKHUD.sharedHUD.contentView,因此我认为扩展PKHUD会更好。

如果您只想更改字体颜色,这会有些麻烦...

概括

关于Swift HUD库PKHUD
印象是它基本上简单易用。
我已经很久没有使用SVProgressHUD了,但是我想使用Swift制作的PKHUD。