Advertisement
andreashorberg

RefreshButton

Oct 15th, 2019
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.14 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct RefreshButton: View {
  4.     @ObservedObject var model: RefreshButtonModel
  5.     let action: () -> Void
  6.     private let animationDuration = 1.0
  7.     private var foreverAnimation: Animation {
  8.         Animation.linear(duration: self.animationDuration)
  9.             .repeatForever(autoreverses: false)
  10.     }
  11.  
  12.     private var refreshImage: some View {
  13.         Image(systemName: "arrow.counterclockwise.circle")
  14.             .font(.system(.title))
  15.     }
  16.  
  17.     private var rotatingRefreshImage: some View {
  18.         refreshImage
  19.             .rotationEffect(.degrees(self.model.isRotating ? 0 : 360))
  20.             .onAppear {
  21.                 withAnimation(self.foreverAnimation) {
  22.                     self.model.isRotating = true
  23.                 }
  24.             }
  25.     }
  26.  
  27.     var body: some View {
  28.         Button(action: {
  29.             self.model.showRotatingButton = true
  30.             self.action()
  31.         }, label: {
  32.             if self.model.showRotatingButton {
  33.                 rotatingRefreshImage
  34.             } else {
  35.                 refreshImage
  36.             }
  37.         })
  38.             .frame(width: 44, height: 44)
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement