Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // gradient.swift
- // AlMall
- //
- // Created by Dima Paliychuk on 11/9/18.
- // Copyright © 2018 Yatseyko Yuriy. All rights reserved.
- //
- import Foundation
- @IBDesignable class GradientView: UIView {
- @IBInspectable var firstColor: UIColor = UIColor.red
- @IBInspectable var secondColor: UIColor = UIColor.green
- @IBInspectable var vertical: Bool = true
- lazy var gradientLayer: CAGradientLayer = {
- let layer = CAGradientLayer()
- layer.colors = [firstColor.cgColor, secondColor.cgColor]
- layer.startPoint = CGPoint.zero
- return layer
- }()
- //MARK: -
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- applyGradient()
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- applyGradient()
- }
- override func prepareForInterfaceBuilder() {
- super.prepareForInterfaceBuilder()
- applyGradient()
- }
- override func layoutSubviews() {
- super.layoutSubviews()
- updateGradientFrame()
- }
- //MARK: -
- func applyGradient() {
- updateGradientDirection()
- layer.sublayers = [gradientLayer]
- }
- func updateGradientFrame() {
- gradientLayer.frame = bounds
- }
- func updateGradientDirection() {
- gradientLayer.endPoint = vertical ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement