6
4

More than 1 year has passed since last update.

【SwiftUI】絵文字が下からいっぱい出てくるアニメーション

Last updated at Posted at 2023-08-30

はじめに

ストーリーに絵文字を送ると下から絵文字が出てくるのをイメージして作ってみました

こんな感じ

Simulator Screen Recording - iPhone 14 Pro - 2023-08-30 at 22.58.10.gif

実装

import SwiftUI

struct ContentView: View {
    @State private var rotation: Double = 0.0

    @State private var isAnimation = false

    var body: some View {
        GeometryReader { geometryProxy in
            ZStack {
                ForEach(0..<50) { _ in
                    emojiView(emoji: "❤️")
                        .scaleEffect(isAnimation ? .random(in: 1...1.5) : 0)
                        .offset(y: isAnimation ? -geometryProxy.size.height : 0)
                        .opacity(isAnimation ? 0 : 1)
                        .rotationEffect(.degrees(isAnimation ? .random(in: -40...40) : 0))
                        .animation(.easeOut(duration: .random(in: 3...6)).delay(.random(in: 0...10)).repeatForever(autoreverses: false), value: isAnimation)
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
            .onAppear {
                isAnimation.toggle()
            }
        }
    }
    
    private func emojiView(emoji: String) -> some View {
        Text(emoji)
            .rotation3DEffect(.degrees(rotation), axis: (x: 0, y: 1, z: 0))
            .font(.system(size: 130))
            .animation(.easeOut(duration: .random(in: 1...2)).repeatForever(autoreverses: false), value: rotation)
            .onAppear { rotation = 360.0 }
    }
}

おわり

インスタの見ながらやったわけじゃなくて完全にイメージだから違うかもw

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