2
2

More than 1 year has passed since last update.

【SwiftUI】Twitterの投稿ボタンを再現する

Posted at

はじめに

Twitterの右下にある投稿ボタンを作ってみます。
押すと色は変化せず、大きさが変化するようなボタンのスタイルです。

サンプルアプリ

Simulator Screen Recording - iPhone 14 Pro - 2023-07-25 at 21.41.20.gif

実装

struct TwitterPostButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .foregroundStyle(.white)
            .frame(width: 60, height: 60)
            .background(.blue, in: Circle())
            .scaleEffect(configuration.isPressed ? 0.9 : 1.0)
            .animation(.spring(), value: configuration.isPressed)
            .shadow(radius: 3)
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)
            .padding(15)
    }
}

extension ButtonStyle where Self == TwitterPostButtonStyle {
    static var twitterPostButton: TwitterPostButtonStyle {
        .init()
    }
}

使い方

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button {
            // タップアクション
        } label: {
            Image(systemName: "plus")
                .resizable()
                .frame(width: 18, height: 18)
        }
        .buttonStyle(.twitterPostButton)
    }
}

おわり

今回は長押しは再現しませんでした。
いい方法が思い浮かばなかったので長押しは機能に追加しませんでした。
長押しのアニメーションもかっこいいのでいつか再現したいです

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