LoginSignup
0
1

More than 1 year has passed since last update.

【swiftUI】UIFeedbackGeneratorでブルッとする振動を実装してみる

Last updated at Posted at 2021-07-18

現在開発中のアプリで使用した感触フィードバックについての解説です。
SNSアプリとかでメッセージ投稿したときブルっと振動がなることってありますよね。それです。

こちらの記事を参考のうえ、swiftUIで実装してみました。
https://qiita.com/WorldDownTown/items/2b5a72e41a95763727bb

Haptics Feedbck(感触フィードバック)とは

iOS10以降で使えるようになった振動によるアクションフィードバック。
ユーザーの何かしらのアクションに対するフィードバックを表現します。

UIImpactFeedbackGenerator, UISelectionFeedbackGenerator, UINotificationFeedbackGenerator
の三種類があるとのことだが、今回はアクションの成功や失敗を表すUINotificationFeedbackGeneratorを実装しました。
(詳しい説明は省略します)
https://developer.apple.com/documentation/uikit/uinotificationfeedbackgenerator

swiftUIで実装してみた

大事な部分以外は省略して書きました


import SwiftUI

struct SettingEditTextView: View {
    let haptics = UINotificationFeedbackGenerator()
    var body: some View {
            Button(action: {
                saveText()
            }, label: {
                Text("Save".uppercased())
            })
        }
        .padding()
        .frame(maxWidth: .infinity)
        .alert(isPresented: $showSuccess) {() -> Alert in
            return Alert(title: Text("更新しました"), message: nil, dismissButton: .default(Text("OK"),action: {
                dismissView()
            }))
        }
    }

    func dismissView() {
        self.haptics.notificationOccurred(.success)
        self.presentaitonMode.wrappedValue.dismiss()
    }

    func saveText() {
        // テキスト保存のメソッド...
        self.showSuccess.toggle()
    }
}

notificationOccurredには.success, .warning, .error の三種類があって、用途によって選択できるとのこと。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1