Created
October 26, 2016 16:50
-
-
Save Quaggie/dee121503ad698bb7be971095245f279 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
import UIKit | |
import Foundation | |
class GradientView: UIView, Gradientable {} | |
protocol Gradientable: class {} | |
extension Gradientable where Self: UIView { | |
private func getCGPoints (for position: GradientPosition, start at: StartAt) -> (CGPoint, CGPoint)? { | |
switch position{ | |
case .diagonal: | |
if at == .lowerRight { | |
return (CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 1)) | |
} else if at == .lowerLeft { | |
return (CGPoint(x: 1, y: 0), CGPoint(x: 0, y: 1)) | |
} else if at == .upperRight { | |
return (CGPoint(x: 0, y: 1), CGPoint(x: 1, y: 0)) | |
} else if at == .upperLeft { | |
return (CGPoint(x: 1, y: 1), CGPoint(x: 0, y: 0)) | |
} else { | |
print("Unaccepted diagonal position") | |
return nil | |
} | |
case .horizontal: | |
if at == .right { | |
return (CGPoint(x: 0, y: 0.5), CGPoint(x: 1, y: 0.5)) | |
} else if at == .left { | |
return (CGPoint(x: 1, y: 0.5), CGPoint(x: 0, y: 0.5)) | |
} else { | |
print("Unaccepted horizontal position") | |
return nil | |
} | |
case .vertical: | |
if at == .top { | |
return (CGPoint(x: 0.5, y: 1), CGPoint(x: 0.5, y: 0)) | |
} else if at == .bottom { | |
return (CGPoint(x: 0.5, y: 0), CGPoint(x: 0.5, y: 1)) | |
} else { | |
print("Unaccepted vertical position") | |
return nil | |
} | |
} | |
} | |
func applyGradient (position: GradientPosition, startAt: StartAt) { | |
let gradientLayer = CAGradientLayer() | |
let cgPoints = getCGPoints(for: position, start: startAt)! | |
gradientLayer.startPoint = cgPoints.0 | |
gradientLayer.endPoint = cgPoints.1 | |
gradientLayer.frame = self.bounds | |
gradientLayer.locations = [0.0] | |
gradientLayer.colors = [Colors.blue, Colors.lightBlue, Colors.lighterBlue,].map { $0.cgColor as CGColor } | |
self.layer.insertSublayer(gradientLayer, at: 0) | |
} | |
func applyGradient (colors: [UIColor], position: GradientPosition, startAt: StartAt) { | |
let gradientLayer = CAGradientLayer() | |
let cgPoints = getCGPoints(for: position, start: startAt)! | |
gradientLayer.startPoint = cgPoints.0 | |
gradientLayer.endPoint = cgPoints.1 | |
gradientLayer.frame = self.bounds | |
gradientLayer.locations = [0.0] | |
gradientLayer.colors = colors.map { $0.cgColor as CGColor } | |
self.layer.insertSublayer(gradientLayer, at: 0) | |
} | |
} | |
enum GradientPosition { | |
case vertical | |
case horizontal | |
case diagonal | |
} | |
enum StartAt { | |
// Vertical | |
case top | |
case bottom | |
// Horizontal | |
case left | |
case right | |
// Diagonal | |
case upperRight | |
case upperLeft | |
case lowerRight | |
case lowerLeft | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment