LoginSignup
6
2

【SwiftUI】iOS17からグラデーションがアニメーションするようになってた

Posted at

はじめに

iOS17からグラデーションがアニメーションするようになってました。

サンプルアプリ

GIFだとわかりにくいですが、iOS16以前ではwithAnimationありでも一瞬で切り替わってしまいます。

~iOS16 iOS17~
Simulator Screen Recording - iPhone 12 - 2023-12-05 at 22.58.14.gif Simulator Screen Recording - iPhone 15 - 2023-12-05 at 22.57.10.gif

実装

import SwiftUI

struct ContentView: View {
    @State private var gradient = LinearGradient(
        colors: [.black],
        startPoint: .top,
        endPoint: .bottom
    )

    var body: some View {
        VStack {
            Button {
                withAnimation {
                    gradient = LinearGradient(
                        colors: [Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)), Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1))],
                        startPoint: .top,
                        endPoint: .bottom
                    )
                }
            } label: {
                Text("変更")
            }
        }
        .ignoresSafeArea()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Rectangle().foregroundStyle(gradient))
    }
}

おわり

めっちゃ小さい改善ですが、嬉しいですね
時間と共に徐々に背景グラデーションが変化していくとかもできそうです。

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