Created
April 3, 2024 22:29
-
-
Save Moussago/c906b5a36c742c1fd58ac0cb209ab8d6 to your computer and use it in GitHub Desktop.
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
// | |
// ContentView.swift | |
// Emoji Animation hero picker | |
// | |
// Created by Moussa on 3/4/2024. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
EmojisAnimatedPicker() | |
} | |
} | |
struct EmojisAnimatedPicker: View { | |
let emojis = Emoji.create() | |
@State private var selection = [Emoji]() | |
@Namespace private var namespace | |
let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 5) | |
var body: some View { | |
VStack(alignment: .leading, spacing: 20) { | |
ScrollView { | |
LazyVGrid(columns: columns, spacing: 10) { | |
ForEach(selection) { item in | |
Text(item.content) | |
.foregroundColor(.clear) | |
.matchedGeometryEffect(id: item.id, in: namespace, isSource: selection.contains(item)) | |
} | |
} | |
.padding() | |
} | |
Spacer() | |
Divider() | |
HStack { | |
ForEach(emojis) { item in | |
Text(item.content) | |
.matchedGeometryEffect(id: item.id, in: namespace, isSource: !selection.contains(item)) | |
.onTapGesture { | |
withAnimation(.spring()) { | |
tapEmoji(item) | |
} | |
} | |
} | |
} | |
.padding() | |
} | |
} | |
func tapEmoji(_ emoji: Emoji) { | |
if let index = selection.firstIndex(of: emoji) { | |
selection.remove(at: index) | |
} else { | |
selection.append(emoji) | |
} | |
} | |
} | |
struct Emoji: Identifiable, Hashable { | |
let id = UUID() | |
let content: String | |
static func create() -> [Emoji] { | |
["πͺ", "π", "π", "πΏ", "π", "π", "π", "π", "π", "πΆ", "π"].map { Emoji(content: $0) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment