8
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?

More than 1 year has passed since last update.

Qiita全国学生対抗戦Advent Calendar 2023

Day 5

【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))
    }
}

おわり

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

8
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
8
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?