【Swift】PKHUDをSwiftUIで使う

# はじめに
UIKitで書かれたライブラリをSwiftUIで使うシリーズの第2弾です

今回は「[PKHUD](https://github.com/pkluz/PKHUD)」こちらのライブラリです。

PKHUDはアラートライブラリでapple純正アラートのようなUIなのでとてもかっこいいです。

↓ こんな感じ ↓
![PKHUD.gif](https://cloud.githubusercontent.com/assets/1275218/10124182/09f4c406-654f-11e5-9cab-0f2e6f470887.gif)

# 実装
```PKHUDView.swift
import SwiftUI
import PKHUD

struct PKHUDView: UIViewRepresentable {
    @Binding var isPresented: Bool
    var HUDContent: HUDContentType
    var delay: Double
    func makeUIView(context: UIViewRepresentableContext<PKHUDView>) -> UIView {
        return UIView()
    }
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PKHUDView>) {
        if isPresented {
            HUD.flash(HUDContent, delay: delay) { finished in
                isPresented = false
            }
        }
    }
}
```
```PKHUDViewModifier.swift
import SwiftUI
import PKHUD

struct PKHUDViewModifier<Parent: View>: View {
    @Binding var isPresented: Bool
    var HUDContent: HUDContentType
    var delay: Double
    var parent: Parent
    var body: some View {
        ZStack {
            parent
            if isPresented {
                PKHUDView(isPresented: $isPresented, HUDContent: HUDContent, delay: delay)
            }
        }
    }
}
```
```PKHUD.swift
import SwiftUI
import PKHUD

extension View {
    public func PKHUD(isPresented: Binding<Bool>, HUDContent: HUDContentType, delay: Double) -> some View {
        PKHUDViewModifier(isPresented: isPresented, HUDContent: HUDContent, delay: delay, parent: self)
    }
}
```
# 使い方
:::note warn
注意
`.PKHUD(...)`は1番外側につけてください。
:::
### progress
![progress_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/fe24950d-d2ce-43c7-07d7-87ed1faa51ee.gif)
```ContentView.swift
struct ContentView: View {
    @State var progress: Bool = false
    var body: some View {
        VStack {
            Button("progress") {
                progress.toggle()
            }
        }
        .PKHUD(isPresented: $progress, HUDContent: .progress, delay: 1.0)
    }
}
```
### systemActivity
![systemActivity_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/e718ffa8-eea5-0757-0e38-a4ea4fce7090.gif)
```ContentView.swift
struct ContentView: View {
    @State var systemActivity: Bool = false
    var body: some View {
        VStack {
            Button("systemActivity") {
                systemActivity.toggle()
            }
        }
        .PKHUD(isPresented: $systemActivity, HUDContent: .systemActivity, delay: 1.0)
    }
}
```

### success
![success_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/e412fe64-f5e6-b7da-b8b7-a6a0afa7a9be.gif)
```ContentView.swift
struct ContentView: View {
    @State var success: Bool = false
    var body: some View {
        VStack {
            Button("success") {
                success.toggle()
            }
        }
        .PKHUD(isPresented: $success, HUDContent: .success, delay: 1.0)
    }
}
```
### error
![error_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/3963dcfb-b9b6-f97a-e4cf-9e2ded6b8c6c.gif)
```ContentView.swift
struct ContentView: View {
    @State var error: Bool = false
    var body: some View {
        VStack {
            Button("error") {
                error.toggle()
            }
        }
        .PKHUD(isPresented: $error, HUDContent: .error, delay: 1.0)
    }
}
```

### label
![label_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/5d3815a7-f4c6-c21b-71c2-65b3c0fc8d41.gif)
```ContentView.swift
struct ContentView: View {
    @State var label: Bool = false
    var body: some View {
        VStack {
            Button("label") {
                label.toggle()
            }
        }
        .PKHUD(isPresented: $label, HUDContent: .label, delay: 1.0)
    }
}
```

### image
![image_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/08549967-42cb-fdcc-f68e-968e31e00f7e.gif)
```ContentView.swift
struct ContentView: View {
    @State var image: Bool = false
    var body: some View {
        VStack {
            Button("image") {
                image.toggle()
            }
        }
        .PKHUD(isPresented: $image, HUDContent: .image, delay: 1.0)
    }
}
```

### labeledProgress
![labeledProgress_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/cbe18400-09aa-00f4-19b9-10f72d3d2f8b.gif)
```ContentView.swift
struct ContentView: View {
    @State var labeledProgress: Bool = false
    var body: some View {
        VStack {
            Button("labeledProgress") {
                labeledProgress.toggle()
            }
        }
        .PKHUD(isPresented: $labeledProgress, HUDContent: .labeledProgress, delay: 1.0)
    }
}
```

### labeledSuccess
![labeledSuccess_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/b73793bb-d7aa-c38b-b2fe-b08e4c54285e.gif)
```ContentView.swift
struct ContentView: View {
    @State var labeledSuccess: Bool = false
    var body: some View {
        VStack {
            Button("labeledSuccess") {
                labeledSuccess.toggle()
            }
        }
        .PKHUD(isPresented: $labeledSuccess, HUDContent: .labeledSuccess, delay: 1.0)
    }
}
```

### labeledError
![labeledError_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/0d422d99-4c17-cc4c-3035-fa52b756ca3d.gif)
```ContentView.swift
struct ContentView: View {
    @State var labeledError: Bool = false
    var body: some View {
        VStack {
            Button("labeledError") {
                labeledError.toggle()
            }
        }
        .PKHUD(isPresented: $labeledError, HUDContent: .labeledError, delay: 1.0)
    }
}
```

### labeledImage
![labeledImage_AdobeCreativeCloudExpress.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/1c4f589f-76a8-3d07-48b1-478d8eb8019b.gif)
```ContentView.swift
struct ContentView: View {
    @State var labeledError: Bool = false
    var body: some View {
        VStack {
            Button("labeledImage") {
                labeledImage.toggle()
            }
        }
        .PKHUD(isPresented: $labeledImage, HUDContent: .labeledImage, delay: 1.0)
    }
}
```

### labeledRotatingImage
![labeledRotatingImage_AdobeCreativeCloudExpress (1).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1745371/34f3af06-4859-fbdb-b542-ac3f518432ea.gif)
```ContentView.swift
struct ContentView: View {
    @State var labeledRotatingImage: Bool = false
    var body: some View {
        VStack {
            Button("labeledRotatingImage") {
                labeledRotatingImage.toggle()
            }
        }
        .PKHUD(isPresented: $labeledRotatingImage, HUDContent: .labeledRotatingImage, delay: 1.0)
    }
}
```

# おわり
サンプルプロジェクトです
参考にしてください。

https://github.com/SNQ-2001/SwiftUI-PKHUD-Demo