0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】ボタン押している時に動きをつける方法

Posted at

SwiftUIのbuttonStyleを使用して、ボタンを押している時に動きをつける方法です。

このサンプルではボタンを押している間透明度と大きさを変更してます。
configuration.isPressedで押しているか判定することができます。

SoftPressButtonStyle.swift
struct SoftPressButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 10)
                    .fill(configuration.isPressed ? Color.gray.opacity(0.6) : Color.gray.opacity(0.2))
            )
            .scaleEffect(configuration.isPressed ? 0.9 : 1.0)
    }
}

このスタイルを適用したいボタンには、
.buttonStyle(SoftPressButtonStyle())のように設定することができます。

Videotogif.gif

このように簡単にカスタマイズできるのでデザインの幅が広がりますね。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?