Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import SwiftUI
- struct BlinkDemo : View {
- struct Blink : View {
- let period: TimeInterval
- @State var timer: Timer? = nil
- @State var isVisible: Bool = true
- var body: some View {
- let text = Text("DISCO")
- return Group {
- if isVisible {
- text
- } else {
- text.opacity(0)
- }
- }.onAppear {
- self.timer = Timer.scheduledTimer(withTimeInterval: self.period, repeats: true) { _ in
- self.isVisible.toggle()
- }
- }.onDisappear {
- self.timer?.invalidate()
- self.timer = nil
- }
- }
- }
- @State var period: TimeInterval = 1.0;
- var body: some View {
- HStack {
- Button(action: self.slowDown) {
- Text("Slow down")
- }
- Blink(period: self.period)
- Button(action: self.speedUp) {
- Text("Speed up")
- }
- }
- }
- func speedUp() {
- self.period *= 0.8;
- }
- func slowDown() {
- self.period *= 1.25;
- }
- }
- #if DEBUG
- struct Blink_Previews : PreviewProvider {
- static var previews: some View {
- BlinkDemo()
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement