Last active
October 29, 2020 18:59
-
-
Save aheze/b6af5f068a77e04bea09dc120c0f119d to your computer and use it in GitHub Desktop.
This file contains 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
func placeHighlights(atTheseLocations rectangles: [CGRect]) { | |
/// First, we'll remove all the existing highlights in the drawingView | |
for highlight in drawingView.subviews { | |
/// Fading it out | |
UIView.animate(withDuration: 0.3, animations: { | |
highlight.alpha = 0 | |
}) { _ in | |
/// Then removing it once it's finished fading | |
highlight.removeFromSuperview() | |
} | |
} | |
/// we're going to loop through the rectangles, adding a highlight "in real life" every time | |
for rectangle in rectangles { | |
/// we make a new UIView | |
let highlight = UIView(frame: CGRect(x: rectangle.origin.x, y: rectangle.origin.y, width: rectangle.width, height: rectangle.height)) | |
/// alpha is 0 right now because we're going to fade it in | |
highlight.alpha = 0 | |
/// we're going to draw a rounded rectangle | |
let newLayer = CAShapeLayer() | |
let layerRect = CGRect(x: 0, y: 0, width: rectangle.width, height: rectangle.height) | |
newLayer.bounds = layerRect | |
newLayer.path = UIBezierPath(roundedRect: layerRect, cornerRadius: rectangle.height / 3.5).cgPath | |
newLayer.lineWidth = 3 | |
/// set the position of the rounded rectangle (by default it would appear centered in the upper left corner, we don't want that) | |
let x = newLayer.bounds.size.width / 2 | |
let y = newLayer.bounds.size.height / 2 | |
newLayer.position = CGPoint(x: x, y: y) | |
/// Color of the highlights | |
newLayer.fillColor = #colorLiteral(red: 0.6502560775, green: 0.9603669892, blue: 1, alpha: 0.3) | |
newLayer.strokeColor = #colorLiteral(red: 0, green: 0.6823529412, blue: 0.937254902, alpha: 1) | |
/// Add the rounded rectangle to the highlight (a UIView) | |
highlight.layer.addSublayer(newLayer) | |
/// we'll add the highlight to the drawingView now | |
drawingView.addSubview(highlight) | |
/// and finally, we'll fade it in! | |
UIView.animate(withDuration: 0.3, animations: { | |
highlight.alpha = 1 | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment