Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emmasteimann/5b9352a64878a7a18135a737981c7459 to your computer and use it in GitHub Desktop.
Save emmasteimann/5b9352a64878a7a18135a737981c7459 to your computer and use it in GitHub Desktop.
GradientView
//
// GradientView.swift
// Aura
//
// Created by Egor Sakhabaev on 23.07.17.
// Copyright © 2017 Egor Sakhabaev. All rights reserved.
//
import UIKit
@IBDesignable
class GradientView: UIView {
@IBInspectable var firstColor: UIColor = UIColor.clear {
didSet {
updateView()
}
}
@IBInspectable var secondColor: UIColor = UIColor.clear {
didSet {
updateView()
}
}
@IBInspectable var isHorizontal: Bool = true {
didSet {
updateView()
}
}
override class var layerClass: AnyClass {
get {
return CAGradientLayer.self
}
}
func updateView() {
let layer = self.layer as! CAGradientLayer
layer.colors = [firstColor, secondColor].map {$0.cgColor}
if (isHorizontal) {
layer.startPoint = CGPoint(x: 0, y: 0.5)
layer.endPoint = CGPoint (x: 1, y: 0.5)
} else {
layer.startPoint = CGPoint(x: 0.5, y: 0)
layer.endPoint = CGPoint (x: 0.5, y: 1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment