Skip to content

Instantly share code, notes, and snippets.

@shaundon
Last active September 6, 2022 08:59
Show Gist options
  • Save shaundon/88e58e7a87fb9c2e7b8f5fa319826c89 to your computer and use it in GitHub Desktop.
Save shaundon/88e58e7a87fb9c2e7b8f5fa319826c89 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ScrollingEmojiView: View {
@Environment(\.accessibilityReduceMotion) var reduceMotion
@State private var isAnimating = false
/*
In Personal Best (getpersonalbest.com) I get a random emoji to represent a workout type,
using an extension to HKWorkoutActivityType. Here I've stubbed it out with a more basic
implementation.
*/
func randomEmoji() -> String {
let character: Character = "🥇🍯👨‍👨‍👦👀⭐️❤️🤓💋".randomElement()!
return String(character)
}
func xOffset(forRowIndex rowIndex: Int) -> Double {
return rowIndex % 2 == 0 ? 100 : -100
}
func randomEmojis() -> [[String]] {
var previouslyChosenEmojis: Set<String> = []
return (0...10).map { _ in
(0...10).map { _ in
var emoji = randomEmoji()
while previouslyChosenEmojis.contains(emoji) {
emoji = randomEmoji()
}
previouslyChosenEmojis.insert(emoji)
return emoji
}
}
}
var body: some View {
let chosenEmojis = emojis
GeometryReader { geo in
VStack {
ForEach(0..<rows, id: \.self) { index in
Text(chosenEmojis[index].joined(separator: " "))
.fixedSize(horizontal: true, vertical: false)
.font(.title)
.offset(x: xOffset(forRowIndex: index), y: 0)
.padding(.vertical, 5)
.animation(.easeInOut(duration: 20).repeatForever(autoreverses: true), value: isAnimating)
}
}
.rotationEffect(.degrees(-15))
.frame(width: geo.size.width, height: geo.size.height)
.fixedSize()
.clipped()
}
.task {
if !reduceMotion {
isAnimating = true
}
}
}
}
struct ScrollingEmojiView_Previews: PreviewProvider {
static var previews: some View {
ScrollingEmojiView()
.edgesIgnoringSafeArea(.all)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment