Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rowwingman/52fc41f45b55dd6d4074cd016d7d167b to your computer and use it in GitHub Desktop.
Save rowwingman/52fc41f45b55dd6d4074cd016d7d167b to your computer and use it in GitHub Desktop.

Revisions

  1. Banck created this gist Oct 3, 2017.
    50 changes: 50 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    //
    // 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)
    }
    }

    }