Skip to content

Instantly share code, notes, and snippets.

@thexande
Last active January 14, 2017 17:25
Show Gist options
  • Save thexande/9f93b3c899af63108415936bf13a43da to your computer and use it in GitHub Desktop.
Save thexande/9f93b3c899af63108415936bf13a43da to your computer and use it in GitHub Desktop.
creates a zig zag view border inside of a swift playground. Create a new playground, and paste in the following code:
import UIKit
import PlaygroundSupport
let uiview = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
func pathZigZagForView(givenView: UIView) ->UIBezierPath {
let width = givenView.frame.size.width
let height = givenView.frame.size.height
let givenFrame = givenView.frame
let zigZagWidth = CGFloat(7)
let zigZagHeight = CGFloat(5)
var yInitial = height-zigZagHeight
let zigZagPath = UIBezierPath(rect: givenFrame.insetBy(dx: 5, dy: 5))
zigZagPath.move(to: CGPoint(x:0, y:0))
zigZagPath.addLine(to: CGPoint(x:0, y:yInitial))
var slope = -1
var x = CGFloat(0)
var i = 0
while x < width {
x = zigZagWidth * CGFloat(i)
let p = zigZagHeight * CGFloat(slope) - 5
let y = yInitial + p
let point = CGPoint(x: x, y: y)
zigZagPath.addLine(to: point)
slope = slope*(-1)
i += 1
}
zigZagPath.addLine(to: CGPoint(x:width,y: 0))
yInitial = 0 + zigZagHeight
x = CGFloat(width)
i = 0
while x > 0 {
x = width - (zigZagWidth * CGFloat(i))
let p = zigZagHeight * CGFloat(slope) + 5
let y = yInitial + p
let point = CGPoint(x: x, y: y)
zigZagPath.addLine(to: point)
slope = slope*(-1)
i += 1
}
zigZagPath.close()
return zigZagPath
}
func applyZigZagEffect(givenView: UIView) {
let shapeLayer = CAShapeLayer(layer: givenView.layer)
givenView.backgroundColor = UIColor.clear
shapeLayer.path = pathZigZagForView(givenView: givenView).cgPath
shapeLayer.frame = givenView.bounds
shapeLayer.fillColor = UIColor.red.cgColor
shapeLayer.masksToBounds = true
shapeLayer.shadowOpacity = 1
shapeLayer.shadowColor = UIColor.black.cgColor
shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
shapeLayer.shadowRadius = 3
givenView.layer.addSublayer(shapeLayer)
}
applyZigZagEffect(givenView: uiview)
PlaygroundPage.current.liveView = uiview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment