Created
May 16, 2023 19:52
-
-
Save MortenGregersen/1a8c9abb02e3f7d943891c848b3ed9e2 to your computer and use it in GitHub Desktop.
An animating streamer view made with SwiftUI as seen here: https://mastodon.social/@mortengregersen/110372090226655546
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
// | |
// HaikuStreamView.swift | |
// HeyAIku | |
// | |
// Created by Morten Bjerg Gregersen on 15/05/2023. | |
// | |
import SwiftUI | |
struct HaikuStreamView: View { | |
@State private var fadingState = FadingState.initial | |
@State private var haikuIndex = 0 | |
private static let tick = 0.5 | |
private let timer = Timer.publish(every: tick, on: .main, in: .common).autoconnect() | |
var body: some View { | |
VStack { | |
Text(haikus[haikuIndex]) | |
.opacity(fadingState.opacity) | |
.offset(x: fadingState.offset) | |
.overlay(alignment: .leading) { | |
Rectangle() | |
.opacity(fadingState.opacity) | |
.frame(width: 1.5) | |
.offset(x: -10 + fadingState.offset) | |
} | |
} | |
.onReceive(timer) { _ in | |
let newFadingState: FadingState | |
switch fadingState { | |
case .initial: newFadingState = .inFocus(0) | |
case .inFocus(let seconds): newFadingState = seconds == 8 ? .hidden : .inFocus(seconds + 1) | |
case .hidden: newFadingState = .inFocus(0) | |
} | |
withAnimation(.easeInOut(duration: Self.tick)) { | |
fadingState = newFadingState | |
} | |
DispatchQueue.main.asyncAfter(deadline: .now() + Self.tick + 0.1) { | |
if newFadingState == FadingState.hidden { | |
haikuIndex = haikuIndex == haikus.count - 1 ? 0 : haikuIndex + 1 | |
fadingState = .initial | |
} | |
} | |
} | |
} | |
private enum FadingState: Equatable { | |
case initial | |
case inFocus(Int) | |
case hidden | |
var opacity: CGFloat { | |
switch self { | |
case .initial: return 0 | |
case .inFocus: return 1 | |
case .hidden: return 0 | |
} | |
} | |
var offset: CGFloat { | |
switch self { | |
case .initial: return 50 | |
case .inFocus: return 0 | |
case .hidden: return -50 | |
} | |
} | |
} | |
private let haikus = [ | |
""" | |
Haiku made easy | |
With AI as your guide now | |
Let's write poems, friend! | |
""", | |
""" | |
Haiku at your hand | |
AI helps you find the words | |
Poetry made grand | |
""", | |
""" | |
Haiku with some help | |
AI adds a modern twist | |
Let's write poetry | |
""", | |
""" | |
Writing haiku, hard? | |
Let the AI lend a hand | |
Creative, you'll be | |
""", | |
""" | |
Poetry made new | |
AI helps you find your voice | |
Haiku just for you | |
""", | |
""" | |
Writing haiku's, tough? | |
But with AI, it's a breeze | |
Creative, you'll be | |
""", | |
""" | |
Let AI help out | |
Haiku poetry made cool | |
Writing's now a snap | |
""", | |
""" | |
Create haiku now | |
With AI as your partner | |
Poetry's reborn | |
""" | |
] | |
} | |
struct HaikuStreamView_Previews: PreviewProvider { | |
static var previews: some View { | |
HaikuStreamView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment