LoginSignup
1
1

More than 1 year has passed since last update.

【SwiftUI】Gaugeを使ってみた

Posted at

はじめに

GaugeはiOS16から使用できるUIコンポーネントです。
初めて触ったので記録しておきます。

どんなものか

Simulator Screen Recording - iPhone 14 - 2022-11-12 at 18.03.29.gif

スタイル

accessoryCircular

Simulator Screen Recording - iPhone 14 - 2022-11-12 at 18.05.16.gif

import SwiftUI

struct ContentView: View {
    @State var value: Double = 0
    var body: some View {
        VStack(spacing: 50) {
            Gauge(value: value, in: 0...100) {
                Text(Int(value), format: .percent)
            }
            .gaugeStyle(.accessoryCircular)
            .tint(.red)

            Slider(value: $value, in: 0...100)
        }
        .padding(.horizontal, 50)
    }
}

accessoryCircularCapacity

Simulator Screen Recording - iPhone 14 - 2022-11-12 at 18.09.46.gif

import SwiftUI

struct ContentView: View {
    @State var value: Double = 0
    var body: some View {
        VStack(spacing: 50) {
            Gauge(value: value, in: 0...100) {
                Text(Int(value), format: .percent)
            }
            .gaugeStyle(.accessoryCircularCapacity)
            .tint(.pink)

            Slider(value: $value, in: 0...100)
        }
        .padding(.horizontal, 50)
    }
}

accessoryLinear

Simulator Screen Recording - iPhone 14 - 2022-11-12 at 18.11.55.gif

import SwiftUI

struct ContentView: View {
    @State var value: Double = 0
    var body: some View {
        VStack(spacing: 50) {
            Gauge(value: value, in: 0...100) {
                Text(Int(value), format: .percent)
            }
            .gaugeStyle(.accessoryLinear)
            .tint(.yellow)

            Slider(value: $value, in: 0...100)
        }
        .padding(.horizontal, 50)
    }
}

accessoryLinearCapacity

Simulator Screen Recording - iPhone 14 - 2022-11-12 at 18.14.11.gif

import SwiftUI

struct ContentView: View {
    @State var value: Double = 0
    var body: some View {
        VStack(spacing: 50) {
            Gauge(value: value, in: 0...100) {
                Text(Int(value), format: .percent)
            }
            .gaugeStyle(.accessoryLinearCapacity)
            .tint(.orange)

            Slider(value: $value, in: 0...100)
        }
        .padding(.horizontal, 50)
    }
}

linearCapacity

Simulator Screen Recording - iPhone 14 - 2022-11-12 at 18.15.28.gif

import SwiftUI

struct ContentView: View {
    @State var value: Double = 0
    var body: some View {
        VStack(spacing: 50) {
            Gauge(value: value, in: 0...100) {
                Text(Int(value), format: .percent)
            }
            .gaugeStyle(.linearCapacity)
            .tint(.purple)

            Slider(value: $value, in: 0...100)
        }
        .padding(.horizontal, 50)
    }
}

大きさの変更

accessoryCircular もしくは accessoryCircularCapacity

    Gauge(value: value, in: 0...100) {
        Text(Int(value), format: .percent)
    }
    .gaugeStyle(.accessoryCircular)
+   .scaleEffect(3)
    Gauge(value: value, in: 0...100) {
        Text(Int(value), format: .percent)
    }
    .gaugeStyle(.accessoryCircularCapacity)
+   .scaleEffect(3)

それ以外

accessoryCircularaccessoryCircularCapacity以外はframeが使用できます。

    Gauge(value: value, in: 0...100) {
        Text(Int(value), format: .percent)
    }
    .gaugeStyle(.accessoryLinear)
    .tint(.yellow)
+   .frame(width: 100)

色の変更

    Gauge(value: value, in: 0...100) {
        Text(Int(value), format: .percent)
    }
+   .tint(.purple)

おわり

めっちゃおしゃれなのでこれを使ったらかっこいいアプリ作れそうです笑

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