Advertisement
mpokhylets

Untitled

Jun 18th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.35 KB | None | 0 0
  1. import Foundation
  2. import SwiftUI
  3.  
  4. struct BlinkDemo : View {
  5.    
  6.     struct Blink : View {
  7.         let period: TimeInterval
  8.         @State var timer: Timer? = nil
  9.         @State var isVisible: Bool = true
  10.  
  11.         var body: some View {
  12.             let text = Text("DISCO")
  13.             return Group {
  14.                 if isVisible {
  15.                     text
  16.                 } else {
  17.                     text.opacity(0)
  18.                 }
  19.             }.onAppear {
  20.                 self.timer = Timer.scheduledTimer(withTimeInterval: self.period, repeats: true) { _ in
  21.                     self.isVisible.toggle()
  22.                 }
  23.             }.onDisappear {
  24.                 self.timer?.invalidate()
  25.                 self.timer = nil
  26.             }
  27.         }
  28.     }
  29.  
  30.     @State var period: TimeInterval = 1.0;
  31.    
  32.     var body: some View {
  33.         HStack {
  34.             Button(action: self.slowDown) {
  35.                 Text("Slow down")
  36.             }
  37.             Blink(period: self.period)
  38.             Button(action: self.speedUp) {
  39.                 Text("Speed up")
  40.             }
  41.         }
  42.     }
  43.  
  44.     func speedUp() {
  45.         self.period *= 0.8;
  46.     }
  47.  
  48.     func slowDown() {
  49.         self.period *= 1.25;
  50.     }
  51. }
  52.  
  53. #if DEBUG
  54. struct Blink_Previews : PreviewProvider {
  55.     static var previews: some View {
  56.         BlinkDemo()
  57.     }
  58. }
  59. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement