AnimatedSwitch.swift 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
//
//  AnimatedSwitch.swift
//  lottie-swift
//
//  Created by Brandon Withrow on 2/4/19.
//

#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

// MARK: - AnimatedSwitch

/// An interactive switch with an 'On' and 'Off' state. When the user taps on the
/// switch the state is toggled and the appropriate animation is played.
///
/// Both the 'On' and 'Off' have an animation play range associated with their state.
///
/// Also available as a SwiftUI view (`LottieSwitch`).
open class AnimatedSwitch: AnimatedControl {

  // MARK: Lifecycle

  public override init(
    animation: LottieAnimation?,
    configuration: LottieConfiguration = .shared)
  {
    /// Generate a haptic generator if available.
    #if os(iOS)
    hapticGenerator = HapticGenerator()
    #else
    hapticGenerator = NullHapticGenerator()
    #endif
    super.init(animation: animation, configuration: configuration)

    #if canImport(UIKit)
    isAccessibilityElement = true
    #elseif canImport(AppKit)
    setAccessibilityElement(true)
    #endif

    updateOnState(isOn: _isOn, animated: false, shouldFireHaptics: false)
  }

  public override init() {
    /// Generate a haptic generator if available.
    #if os(iOS)
    hapticGenerator = HapticGenerator()
    #else
    hapticGenerator = NullHapticGenerator()
    #endif

    super.init()

    #if canImport(UIKit)
    isAccessibilityElement = true
    #elseif canImport(AppKit)
    setAccessibilityElement(true)
    #endif

    updateOnState(isOn: _isOn, animated: false, shouldFireHaptics: false)
  }

  required public init?(coder aDecoder: NSCoder) {
    /// Generate a haptic generator if available.
    #if os(iOS)
    hapticGenerator = HapticGenerator()
    #else
    hapticGenerator = NullHapticGenerator()
    #endif
    super.init(coder: aDecoder)

    #if canImport(UIKit)
    isAccessibilityElement = true
    #elseif canImport(AppKit)
    setAccessibilityElement(true)
    #endif
  }

  // MARK: Open

  open override func animationDidSet() {
    updateOnState(isOn: _isOn, animated: animateUpdateWhenChangingAnimation, shouldFireHaptics: false)
  }

  #if canImport(UIKit)
  open override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
    super.endTracking(touch, with: event)
    updateOnState(isOn: !_isOn, animated: true, shouldFireHaptics: true)
    sendActions(for: .valueChanged)
  }

  #elseif canImport(AppKit)
  open override func handle(_ event: LottieNSControlEvent) {
    super.handle(event)

    if event == .touchUpInside {
      updateOnState(isOn: !_isOn, animated: true, shouldFireHaptics: true)
    }
  }
  #endif

  // MARK: Public

  /// Defines what happens when the user taps the switch while an
  /// animation is still in flight
  public enum CancelBehavior {
    case reverse // default - plays the current animation in reverse
    case none // does not update the animation when canceled
  }

  /// The cancel behavior for the switch. See CancelBehavior for options
  public var cancelBehavior: CancelBehavior = .reverse

  /// If `false` the switch will not play the animation when changing between animations.
  public var animateUpdateWhenChangingAnimation = true

  #if canImport(UIKit)
  public override var accessibilityTraits: UIAccessibilityTraits {
    set { super.accessibilityTraits = newValue }
    get { super.accessibilityTraits.union(.button) }
  }
  #endif

  /// A closure that is called when the `isOn` state is updated
  public var stateUpdated: ((_ isOn: Bool) -> Void)?

  /// The current state of the switch.
  public var isOn: Bool {
    set {
      /// This is forwarded to a private variable because the animation needs to be updated without animation when set externally and with animation when set internally.
      guard _isOn != newValue else { return }
      updateOnState(isOn: newValue, animated: false, shouldFireHaptics: false)
    }
    get {
      _isOn
    }
  }

  /// Set the state of the switch and specify animation and haptics
  public func setIsOn(_ isOn: Bool, animated: Bool, shouldFireHaptics: Bool = true) {
    guard isOn != _isOn else { return }
    updateOnState(isOn: isOn, animated: animated, shouldFireHaptics: shouldFireHaptics)
  }

  /// Sets the play range for the given state. When the switch is toggled, the animation range is played.
  public func setProgressForState(
    fromProgress: AnimationProgressTime,
    toProgress: AnimationProgressTime,
    forOnState: Bool)
  {
    if forOnState {
      onStartProgress = fromProgress
      onEndProgress = toProgress
    } else {
      offStartProgress = fromProgress
      offEndProgress = toProgress
    }

    updateOnState(isOn: _isOn, animated: false, shouldFireHaptics: false)
  }

  // MARK: Internal

  private(set) var onStartProgress: CGFloat = 0
  private(set) var onEndProgress: CGFloat = 1
  private(set) var offStartProgress: CGFloat = 1
  private(set) var offEndProgress: CGFloat = 0

  // MARK: Animation State

  func updateOnState(isOn: Bool, animated: Bool, shouldFireHaptics: Bool) {
    _isOn = isOn
    var startProgress = isOn ? onStartProgress : offStartProgress
    var endProgress = isOn ? onEndProgress : offEndProgress
    let finalProgress = endProgress

    if cancelBehavior == .reverse {
      let realtimeProgress = animationView.realtimeAnimationProgress

      let previousStateStart = isOn ? offStartProgress : onStartProgress
      let previousStateEnd = isOn ? offEndProgress : onEndProgress
      if
        realtimeProgress.isInRange(
          min(previousStateStart, previousStateEnd),
          max(previousStateStart, previousStateEnd))
      {
        /// Animation is currently in the previous time range. Reverse the previous play.
        startProgress = previousStateEnd
        endProgress = previousStateStart
      }
    }

    updateAccessibilityLabel()

    guard animated == true else {
      animationView.currentProgress = finalProgress
      return
    }

    if shouldFireHaptics {
      hapticGenerator.generateImpact()
    }

    animationView.play(
      fromProgress: startProgress,
      toProgress: endProgress,
      loopMode: LottieLoopMode.playOnce,
      completion: { [weak self] finished in
        guard let self else { return }

        // For the Main Thread rendering engine, we freeze the animation at the expected final progress
        // once the animation is complete. This isn't necessary on the Core Animation engine.
        if finished, !(animationView.animationLayer is CoreAnimationLayer) {
          animationView.currentProgress = finalProgress
        }
      })
  }

  // MARK: Fileprivate

  fileprivate var hapticGenerator: ImpactGenerator

  fileprivate var _isOn = false {
    didSet {
      stateUpdated?(_isOn)
    }
  }

  // MARK: Private

  private func updateAccessibilityLabel() {
    let value = _isOn ? NSLocalizedString("On", comment: "On") : NSLocalizedString("Off", comment: "Off")

    #if canImport(UIKit)
    accessibilityValue = value
    #elseif canImport(AppKit)
    setAccessibilityValue(value)
    #endif
  }

}

// MARK: - ImpactGenerator

protocol ImpactGenerator {
  func generateImpact()
}

#if os(iOS)
class HapticGenerator: ImpactGenerator {

  // MARK: Internal

  func generateImpact() {
    impact.impactOccurred()
  }

  // MARK: Fileprivate

  fileprivate let impact = UIImpactFeedbackGenerator(style: .light)
}
#else
// MARK: - NullHapticGenerator

class NullHapticGenerator: ImpactGenerator {
  func generateImpact() { }
}
#endif