Last active
August 18, 2022 17:36
-
-
Save mattyoung/3fd0e65646295037b11092ac599b07f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import SwiftUI | |
extension BinaryInteger { | |
var evenOddDirection: Double { | |
self.isMultiple(of: 2) ? 1 : -1 | |
} | |
} | |
struct ScrollingEmojiView: View { | |
@State private var direction = 1.0 | |
@Environment(\.accessibilityReduceMotion) var reduceMotion | |
let emojis: [String] = { | |
// change this to however you want to generate random emoji | |
func randomEmoji() -> String { | |
String(allEmojis.randomElement()!.character) | |
} | |
let rows = 15 | |
var previouslyChosenEmoji = "" | |
return (0...rows).map { _ in | |
(0...20).map { _ in | |
var emoji = randomEmoji() | |
while emoji == previouslyChosenEmoji { | |
emoji = randomEmoji() | |
} | |
previouslyChosenEmoji = emoji | |
return emoji | |
} | |
.joined(separator: " ") | |
} | |
}() | |
var body: some View { | |
GeometryReader { geo in | |
VStack(spacing: 10) { | |
ForEach(emojis.indices, id: \.self) { index in | |
Text(emojis[index]) | |
.fixedSize(horizontal: true, vertical: false) | |
.font(.system(size: 50)) | |
.offset(x: reduceMotion ? 0 : direction * index.evenOddDirection * geo.size.width / 2, y: 0) | |
.animation(.linear(duration: 30).repeatForever(autoreverses: true), value: direction) | |
} | |
} | |
.rotationEffect(.degrees(-15)) | |
.frame(width: geo.size.width, height: geo.size.height) | |
.clipped() | |
} | |
.onAppear { | |
direction = -direction | |
} | |
} | |
} | |
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