daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //: A UIKit based Playground for presenting user interface
  2.  
  3. import UIKit
  4. import PlaygroundSupport
  5.  
  6. final class StripedGradientView: UIView {
  7.  
  8.     @available(*, unavailable, message: "This property is reserved for Interface Builder. Use 'direction' instead.")
  9.     @IBInspectable var directionName: String? {
  10.         willSet {
  11.             if let newDirection = Direction(rawValue: newValue ?? "") {
  12.                 self.direction = newDirection
  13.             }
  14.         }
  15.     }
  16.    
  17.     var direction: Direction = .topBottom
  18.    
  19.     override func draw(_ rect: CGRect) {
  20.        
  21.         let thickness: CGFloat = 16
  22.         let gap: CGFloat = 8
  23.         let width = rect.size.width
  24.         let height = rect.size.height
  25.        
  26.         guard let context = UIGraphicsGetCurrentContext() else { return }
  27.         context.setStrokeColor(UIColor.white.withAlphaComponent(0.5).cgColor)
  28.         context.setLineWidth(thickness)
  29.        
  30.         var p = -(width > height ? width : height) - thickness
  31.        
  32.        
  33.         while p <= width {
  34.             print(p)
  35.             let startPointX = direction == .topBottom ? p + thickness + height : p - thickness
  36.             let endPointX = direction == .topBottom ? p - thickness : p + thickness + height
  37.            
  38.             context.move( to: CGPoint(x: startPointX , y: -thickness) )
  39.             context.addLine( to: CGPoint(x: endPointX, y: thickness + height) )
  40.  
  41.             context.strokePath()
  42.             p += gap + thickness * 2
  43.         }
  44.        
  45.         let gradient = CAGradientLayer()
  46.  
  47.         gradient.frame = bounds
  48.         gradient.colors = [UIColor.black.cgColor,
  49.                            UIColor.clear.cgColor]
  50.         layer.mask = gradient
  51.     }
  52.    
  53.     enum Direction: String {
  54.         case bottomTop, topBottom
  55.     }
  56. }
  57.  
  58.  
  59. class MyViewController : UIViewController {
  60.     override func loadView() {
  61.         let view = UIView()
  62.         view.backgroundColor = .white
  63.  
  64.         let stripedView = StripedGradientView()
  65.         stripedView.frame = CGRect(x: 150, y: 200, width: 100, height: 200)
  66.        
  67.         view.addSubview(stripedView)
  68.  
  69.         self.view = view
  70.     }
  71.    
  72.  
  73. }
  74. // Present the view controller in the Live View window
  75. PlaygroundPage.current.liveView = MyViewController()
RAW Paste Data
Top