Created
April 4, 2024 17:16
-
-
Save Moussago/3343ee6cf2146f5ce40cd6b68a6c6743 to your computer and use it in GitHub Desktop.
Matched Geometry animated selection SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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