Skip to content

Instantly share code, notes, and snippets.

@Moussago
Created April 4, 2024 17:16
Show Gist options
  • Save Moussago/3343ee6cf2146f5ce40cd6b68a6c6743 to your computer and use it in GitHub Desktop.
Save Moussago/3343ee6cf2146f5ce40cd6b68a6c6743 to your computer and use it in GitHub Desktop.
Matched Geometry animated selection SwiftUI
//
// EmojisAnimatedView.swift
//
//
// Created by Moussa on 3/4/2024.
//
import Foundation
import SwiftUI
struct ContentView: View {
var body: some View {
EmojisAnimatedView()
}
}
struct EmojisAnimatedView: View {
@Namespace var ns
@State private var selection: Int = 1
var body: some View {
VStack(spacing: 20) {
LazyVGrid(columns: Array(repeating: GridItem(), count: 3), content: {
ForEach(0..<30) { emojiId in
EmojiView(selection: $selection, emoji: emojiId)
.matchedGeometryEffect(id: emojiId, in: ns)
}
})
}.background(
RoundedRectangle(cornerRadius: 8).stroke(Color.cyan, lineWidth: 3)
.matchedGeometryEffect(id: selection, in: ns, isSource: false)
)
}
struct EmojiView: View {
@Binding var selection: Int
let emojis = ["🌿", "🌎", "πŸ”₯", "🌻", "πŸͺ", "🌊", "😁", "🌸", "🌌", "🌲", "πŸ˜„", "🌠", "🌺", "πŸŒ…", "πŸ‰", "🌡", "🌝", "πŸŒ„", "πŸ˜ƒ", "πŸŒ•", "🌼", "🌱", "🌞", "🌧️", "🌲", "πŸŒ‘", "🌾", "πŸ˜‹", "πŸŒ”", "😎"]
let emoji: Int
var body: some View {
Text(emojis[emoji])
.font(.largeTitle)
.padding(10)
.onTapGesture {
withAnimation(.easeInOut(duration: 0.40)) {
self.selection = emoji
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment