Skip to content

Instantly share code, notes, and snippets.

@yoman07
Last active January 5, 2018 14:51
Show Gist options
  • Save yoman07/72dd5ba9d4409a9820c753f5c69a9676 to your computer and use it in GitHub Desktop.
Save yoman07/72dd5ba9d4409a9820c753f5c69a9676 to your computer and use it in GitHub Desktop.
Striped gradient view
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
final class StripedGradientView: UIView {
@available(*, unavailable, message: "This property is reserved for Interface Builder. Use 'direction' instead.")
@IBInspectable var directionName: String? {
willSet {
if let newDirection = Direction(rawValue: newValue ?? "") {
self.direction = newDirection
}
}
}
var direction: Direction = .topBottom
override func draw(_ rect: CGRect) {
let thickness: CGFloat = 16
let gap: CGFloat = 8
let width = rect.size.width
let height = rect.size.height
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setStrokeColor(UIColor.white.withAlphaComponent(0.5).cgColor)
context.setLineWidth(thickness)
var p = -(width > height ? width : height) - thickness
while p <= width {
print(p)
let startPointX = direction == .topBottom ? p + thickness + height : p - thickness
let endPointX = direction == .topBottom ? p - thickness : p + thickness + height
context.move( to: CGPoint(x: startPointX , y: -thickness) )
context.addLine( to: CGPoint(x: endPointX, y: thickness + height) )
context.strokePath()
p += gap + thickness * 2
}
let gradient = CAGradientLayer()
gradient.frame = bounds
gradient.colors = [UIColor.black.cgColor,
UIColor.clear.cgColor]
layer.mask = gradient
}
enum Direction: String {
case bottomTop, topBottom
}
}
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let stripedView = StripedGradientView()
stripedView.frame = CGRect(x: 150, y: 200, width: 100, height: 200)
view.addSubview(stripedView)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment