Skip to content

Instantly share code, notes, and snippets.

@Banck
Created October 3, 2017 08:14
Show Gist options
  • Save Banck/18a20901bcd6fb872a85b56ed71fcd12 to your computer and use it in GitHub Desktop.
Save Banck/18a20901bcd6fb872a85b56ed71fcd12 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)
}
}
}
@bbajra
Copy link

bbajra commented Dec 6, 2018

Used this class, but not being able to show color gradient in my view. I do have the option to select first and second color

@linneverstops
Copy link

Used this file in my class project. Beautiful! Thank you so much!

@amustafa95
Copy link

Thanl you man its realy help

@Surferdude667
Copy link

Awesome! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment