Created
July 4, 2020 02:11
-
-
Save dagronf/7848436ddad92fb520f8824129494817 to your computer and use it in GitHub Desktop.
A simple marching ants CALayer implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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