-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from AckeeCZ/gradient_view
Gradient view.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// GradientView.swift | ||
// ACKategories | ||
// | ||
// Created by Marek Fořt on 10/5/18. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
/** | ||
This view creates a gradient view with the defined colors | ||
- Warning: Please note that if one of your colors is clear, you should usually define to which "clear color" it should go to - i.e. if you want to go from white to clear, write: | ||
|
||
`[UIColor.white, UIColor.white.withAlphaComponent(0)]` | ||
*/ | ||
open class GradientView: UIView { | ||
|
||
private weak var gradientLayer: CAGradientLayer! | ||
|
||
/** | ||
Creates a gradient view with colors and axis | ||
- Parameters: | ||
- colors: The colors to be used for the gradient. | ||
- axis: The axis of the gradient: `.vertical` for bottom-to-top gradient, `.horizontal` for left-to-right gradient. | ||
*/ | ||
public init(colors: [UIColor], axis: NSLayoutConstraint.Axis) { | ||
super.init(frame: CGRect(x: 0, y: 0, width: 1, height: 1)) | ||
let gradientLayer = CAGradientLayer() | ||
gradientLayer.frame = bounds | ||
gradientLayer.colors = colors.map { $0.cgColor } | ||
if axis == .vertical { | ||
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0) | ||
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1) | ||
} else { | ||
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5) | ||
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5) | ||
} | ||
|
||
layer.insertSublayer(gradientLayer, at: 0) | ||
self.gradientLayer = gradientLayer | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override open func layoutSubviews() { | ||
super.layoutSubviews() | ||
gradientLayer.frame = bounds | ||
} | ||
} |
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