Skip to content

Commit

Permalink
Merge pull request #36 from AckeeCZ/gradient_view
Browse files Browse the repository at this point in the history
Gradient view.
  • Loading branch information
janmisar authored Oct 5, 2018
2 parents 18de184 + d61ad17 commit 6eaa609
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ACKategories.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
881A82EF21131D8D00267D2E /* DateFormattingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 881A82ED21131D0F00267D2E /* DateFormattingTests.swift */; };
A3F605B42136F41D005CBE3F /* FlowCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3F605B32136F41D005CBE3F /* FlowCoordinator.swift */; };
D2FF92C4202BA8DE00C1A129 /* NSAttributedStringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */; };
F8CA9D63216755DC00F1AF45 /* GradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8CA9D62216755DB00F1AF45 /* GradientView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -157,6 +158,7 @@
A3F605B32136F41D005CBE3F /* FlowCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlowCoordinator.swift; sourceTree = "<group>"; };
D253FF781F0A65610079215C /* ACKategories.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ACKategories.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSAttributedStringExtensions.swift; sourceTree = "<group>"; };
F8CA9D62216755DB00F1AF45 /* GradientView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -237,6 +239,7 @@
881A82EB21131CEC00267D2E /* DateFormatting.swift */,
6925242720D142E200228289 /* DictionaryExtensions.swift */,
A3F605B32136F41D005CBE3F /* FlowCoordinator.swift */,
F8CA9D62216755DB00F1AF45 /* GradientView.swift */,
D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */,
6939359E2080AB2800EDA582 /* NSMutableParagraphStyleExtensions.swift */,
6925242520D142C800228289 /* NumberFormatterExtensions.swift */,
Expand Down Expand Up @@ -567,6 +570,7 @@
6925242620D142C800228289 /* NumberFormatterExtensions.swift in Sources */,
69306C572029C48300BBE8F0 /* UIControl+Blocks.swift in Sources */,
6925242E20D143DE00228289 /* ArrayExtensions.swift in Sources */,
F8CA9D63216755DC00F1AF45 /* GradientView.swift in Sources */,
69306C562029C48300BBE8F0 /* TableHeaderFooterView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
52 changes: 52 additions & 0 deletions ACKategories/GradientView.swift
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
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ If running on iOS 9 or later you can use implicit parameter `UIControl.primaryAc
### TableHeaderFooterView
Use this view as TableHeaderView or TableFooterView when your table/footer has dynamic content size.

### GradientView
Use this view when you want to create a view with gradient colors and a given axis.

`Init` takes two 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.

Please **note**: 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:
```swift
let gradientView = GradientView(colors: [UIColor.white, UIColor.white.withAlphaComponent(0)], axis: .vertical)
```

### UITableView and UICollectionView extensions
Since now you can use simple extension which autoregisters your `UITableView` and `UICollectionView` cells!
```swift
Expand Down

0 comments on commit 6eaa609

Please sign in to comment.