Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created July 4, 2020 02:11
Show Gist options
  • Save dagronf/7848436ddad92fb520f8824129494817 to your computer and use it in GitHub Desktop.
Save dagronf/7848436ddad92fb520f8824129494817 to your computer and use it in GitHub Desktop.
A simple marching ants CALayer implementation
class MarchingAntsSelectionLayer: CAShapeLayer {
func setup() {
let dashPattern: [NSNumber] = [2.0, 2.0]
let dashPhase = dashPattern.map { $0.floatValue }.reduce(0) { $0 + $1 }
self.lineDashPattern = dashPattern
let lineDashPhaseAnimation = CABasicAnimation(keyPath: "lineDashPhase")
lineDashPhaseAnimation.byValue = dashPhase
lineDashPhaseAnimation.duration = 0.5
lineDashPhaseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
lineDashPhaseAnimation.repeatCount = .greatestFiniteMagnitude
self.add(lineDashPhaseAnimation, forKey: "lineDashPhaseAnimation")
self.path = CGPath(rect: self.bounds, transform: nil)
self.lineWidth = 1.0
self.strokeColor = NSColor.white.cgColor
self.fillColor = NSColor.selectedContentBackgroundColor.cgColor.copy(alpha: 0.2)
}
func setSelectionRect(rect: CGRect) {
CATransaction.begin()
CATransaction.setDisableActions(true)
self.frame = rect
self.path = CGPath(rect: self.bounds, transform: nil)
CATransaction.commit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment