AnimatedControl.swift 6.5 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
//
//  AnimatedControl.swift
//  lottie-swift
//
//  Created by Brandon Withrow on 2/4/19.
//

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

// MARK: - AnimatedControl

/// Lottie comes prepacked with a two Animated Controls, `AnimatedSwitch` and
/// `AnimatedButton`. Both of these controls are built on top of `AnimatedControl`
///
/// `AnimatedControl` is a subclass of `UIControl` that provides an interactive
/// mechanism for controlling the visual state of an animation in response to
/// user actions.
///
/// The `AnimatedControl` will show and hide layers depending on the current
/// `UIControl.State` of the control.
///
/// Users of `AnimationControl` can set a Layer Name for each `UIControl.State`.
/// When the state is change the `AnimationControl` will change the visibility
/// of its layers.
///
/// NOTE: Do not initialize directly. This is intended to be subclassed.
open class AnimatedControl: LottieControlType {

  // MARK: Lifecycle

  // MARK: Initializers

  public init(
    animation: LottieAnimation?,
    configuration: LottieConfiguration = .shared)
  {
    animationView = LottieAnimationView(
      animation: animation,
      configuration: configuration)

    super.init(frame: animation?.bounds ?? .zero)
    commonInit()
  }

  public init() {
    animationView = LottieAnimationView()
    super.init(frame: .zero)
    commonInit()
  }

  required public init?(coder aDecoder: NSCoder) {
    animationView = LottieAnimationView()
    super.init(coder: aDecoder)
    commonInit()
  }

  // MARK: Open

  // MARK: UIControl Overrides

  open override var isEnabled: Bool {
    didSet {
      updateForState()
    }
  }

  #if canImport(UIKit)
  open override var isSelected: Bool {
    didSet {
      updateForState()
    }
  }
  #endif

  open override var isHighlighted: Bool {
    didSet {
      updateForState()
    }
  }

  open override var intrinsicContentSize: CGSize {
    animationView.intrinsicContentSize
  }

  #if canImport(UIKit)
  open override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
    updateForState()
    return super.beginTracking(touch, with: event)
  }

  open override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
    updateForState()
    return super.continueTracking(touch, with: event)
  }

  open override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
    updateForState()
    return super.endTracking(touch, with: event)
  }

  open override func cancelTracking(with event: UIEvent?) {
    updateForState()
    super.cancelTracking(with: event)
  }

  #elseif canImport(AppKit)
  open override func mouseDown(with mouseDownEvent: NSEvent) {
    guard let window else { return }

    currentState = .highlighted
    updateForState()
    handle(LottieControlEvent(mouseDownEvent.type, inside: eventIsInside(mouseDownEvent)))

    // AppKit mouse-tracking loop from:
    // https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html#//apple_ref/doc/uid/10000060i-CH6-SW4
    var keepOn = true
    while
      keepOn,
      let event = window.nextEvent(
        matching: .any,
        until: .distantFuture,
        inMode: .eventTracking,
        dequeue: true)
    {
      if event.type == .leftMouseUp {
        keepOn = false
      }

      let isInside = eventIsInside(event)
      handle(LottieControlEvent(event.type, inside: isInside))

      let expectedState = (isInside && keepOn) ? LottieNSControlState.highlighted : .normal
      if currentState != expectedState {
        currentState = expectedState
        updateForState()
      }
    }
  }

  func handle(_: LottieNSControlEvent) {
    // To be overridden in subclasses
  }

  private func eventIsInside(_ event: NSEvent) -> Bool {
    let mouseLocation = convert(event.locationInWindow, from: nil)
    return isMousePoint(mouseLocation, in: bounds)
  }
  #endif

  open func animationDidSet() { }

  // MARK: Public

  /// The animation view in which the animation is rendered.
  public let animationView: LottieAnimationView

  /// The animation backing the animated control.
  public var animation: LottieAnimation? {
    didSet {
      animationView.animation = animation
      animationView.bounds = animation?.bounds ?? .zero
      #if canImport(UIKit)
      setNeedsLayout()
      #elseif canImport(AppKit)
      needsLayout = true
      #endif
      updateForState()
      animationDidSet()
    }
  }

  /// The speed of the animation playback. Defaults to 1
  public var animationSpeed: CGFloat {
    set { animationView.animationSpeed = newValue }
    get { animationView.animationSpeed }
  }

  /// Sets which Animation Layer should be visible for the given state.
  public func setLayer(named: String, forState: LottieControlState) {
    stateMap[forState.rawValue] = named
    updateForState()
  }

  /// Sets a ValueProvider for the specified keypath
  public func setValueProvider(_ valueProvider: AnyValueProvider, keypath: AnimationKeypath) {
    animationView.setValueProvider(valueProvider, keypath: keypath)
  }

  // MARK: Internal

  var stateMap: [UInt: String] = [:]

  #if canImport(UIKit)
  var currentState: LottieControlState {
    state
  }

  #elseif canImport(AppKit)
  var currentState = LottieControlState.normal
  #endif

  func updateForState() {
    guard let animationLayer = animationView.animationLayer else { return }
    if
      let layerName = stateMap[currentState.rawValue],
      let stateLayer = animationLayer.layer(for: AnimationKeypath(keypath: layerName))
    {
      for layer in animationLayer._animationLayers {
        layer.isHidden = true
      }
      stateLayer.isHidden = false
    } else {
      for layer in animationLayer._animationLayers {
        layer.isHidden = false
      }
    }
  }

  // MARK: Private

  private func commonInit() {
    #if canImport(UIKit)
    animationView.clipsToBounds = false
    clipsToBounds = true
    #endif

    animationView.translatesAutoresizingMaskIntoConstraints = false
    animationView.backgroundBehavior = .forceFinish
    addSubview(animationView)
    animationView.contentMode = .scaleAspectFit

    #if canImport(UIKit)
    animationView.isUserInteractionEnabled = false
    #endif

    animationView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    animationView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
    animationView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    animationView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  }
}