-
-
Save Parietal/2ead3cd3b8514c7d31aac44f8a64e5e1 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 | |
import Combine | |
extension UIScreen{ | |
static let screenWidth = UIScreen.main.bounds.size.width | |
static let screenHeight = UIScreen.main.bounds.size.height | |
static let screenSize = UIScreen.main.bounds.size | |
} | |
let lightUpProtocol = PassthroughSubject<(Int,Int),Never>() | |
struct ContentView: View { | |
let timer = Timer.publish(every: 0.4, on: .main, in: .common).autoconnect() | |
@State var lightUp = 0 | |
@State var column = 0 | |
var body: some View { | |
ZStack { | |
Color.black | |
.onReceive(timer) { value in | |
let rnd = Int.random(in: 0..<32) | |
doIt(column: rnd) | |
} | |
HStack(alignment: .top, content: { | |
ForEach(0..<24) { foo in | |
LetterView(column: foo) | |
} | |
}) | |
} | |
} | |
func doIt(column: Int) { | |
var lightUp = 0 | |
var row = 0 | |
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { timer in | |
row += 1 | |
lightUpProtocol.send((lightUp,column)) | |
if lightUp < 32 { | |
lightUp += 1 | |
} else { | |
lightUp = 0 | |
} | |
if row == 32 { | |
timer.invalidate() | |
} | |
} | |
} | |
} | |
struct LetterView: View { | |
@State var column:Int | |
@State var letters:[String] = [] | |
@State var alpha:[Double] = [] | |
@State var color:[Color] = [] | |
@State var ready = false | |
var body: some View { | |
VStack(alignment: .center, spacing: 0, content: { | |
if !ready { | |
Color.clear | |
.frame(width: 0, height: 0) | |
.onAppear { | |
for _ in 0...32 { | |
let rnd = UInt8.random(in: 65...91) | |
letters.append(String(UnicodeScalar(rnd))) | |
alpha.append(0) | |
color.append(Color.white) | |
} | |
ready = true | |
} | |
} else { | |
ForEach ((0..<letters.count), id: \.self) { dix in | |
Text(letters[dix]) | |
.bold() | |
.foregroundColor(color[dix]) | |
.opacity(alpha[dix]) | |
.onReceive(lightUpProtocol) { value in | |
let (foo,bar) = value | |
if bar == column { | |
if foo == dix { | |
alpha[dix] = 1.0 | |
color[dix] = Color.white | |
} else { | |
color[dix] = Color.green | |
alpha[dix] -= 0.1 | |
} | |
} | |
} | |
} | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment