Skip to content

Instantly share code, notes, and snippets.

@M8Games
Last active December 5, 2016 14:14
Show Gist options
  • Save M8Games/5c605f84d2cfc55f4828 to your computer and use it in GitHub Desktop.
Save M8Games/5c605f84d2cfc55f4828 to your computer and use it in GitHub Desktop.
//
// ShadowView.swift
//
import UIKit
@IBDesignable
class ShadowView: UIView {
@IBInspectable var startAlpha: CGFloat = 90 {
didSet {
setNeedsDisplay()
}
}
@IBInspectable var midAlpha: CGFloat = 45 {
didSet {
setNeedsDisplay()
}
}
@IBInspectable var midPosPercent: CGFloat = 50 {
didSet {
setNeedsDisplay()
}
}
@IBInspectable var endAlpha: CGFloat = 0 {
didSet {
setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
let colorSpace = CGColorSpaceCreateDeviceRGB()
// let scaleFactor: CGFloat = 1
let rectangle = CGRectMake( 0, 0, self.bounds.width, self.bounds.height )
CGContextSaveGState( ctx )
CGContextAddRect( ctx, rectangle )
CGContextClip( ctx )
let gradientColors: [CGFloat] = [
0, 0, 0, self.startAlpha / 100,
0, 0, 0, self.midAlpha / 100,
0, 0, 0, self.endAlpha / 100 ]
let gradientLocations: [CGFloat] = [ 0, self.midPosPercent / 100, 0.9 ]
let gradientRef = CGGradientCreateWithColorComponents( colorSpace, gradientColors, gradientLocations, 3 )
// CGContextDrawLinearGradient( ctx, gradientRef, CGPoint(x: 0, y: 0), CGPoint(x: 0, y: self.bounds.height), CGGradientDrawingOptions(CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation) )
CGContextDrawLinearGradient( ctx, gradientRef, CGPoint(x: 0, y: 0), CGPoint(x: 0, y: self.bounds.height), CGGradientDrawingOptions([.DrawsBeforeStartLocation, .DrawsAfterEndLocation]) )
CGContextRestoreGState( ctx )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment