0
1

【SwiftUI】ボタンの長押ししている間は繰り返し処理を行う(iOS17)

Posted at

はじめに

言葉だけだとイメージしづらいと思うので具体例を出すと、キーボードのバックスペースボタンです。
バックスペースボタンを長押ししている間は文字を消し続けてくれます。

あれを自前で実装しようと思うと割と大変そうです。

iOS17から標準機能として提供され始めたので記事にしておきます。

サンプルアプリ

画面収録-2024-01-06-23.19.12.gif

実装

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button {
            print("りんご")
        } label: {
            Text("長押しで「りんご」と繰り返し出力する")
        }
        .buttonRepeatBehavior(.enabled)
    }
}

disabledを渡すといつも通りのボタンの動きになります。

.buttonRepeatBehavior(.disabled)

おわり

長押しで繰り返し処理ってキーボードのバックスペース以外に使われる箇所あるのかな?

公式ドキュメント

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