Skip to content

Commit

Permalink
Merge pull request #2 from UISugar/GradientLayer
Browse files Browse the repository at this point in the history
Add makeGradientLayer
  • Loading branch information
fomagran authored Sep 12, 2022
2 parents c98af78 + 69f61b6 commit 1822b76
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 12 deletions.
4 changes: 2 additions & 2 deletions EasierPath.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'EasierPath'
s.version = '1.0.1'
s.version = '1.1.0'
s.summary = 'To make it easier to use UIBezierPath ⚡️'


Expand All @@ -25,5 +25,5 @@ TODO: Add long description of the pod here.

s.source_files = 'EasierPath/Classes/**/*'

s.swift_versions = '5.4'
s.swift_versions = '5.0'
end
15 changes: 14 additions & 1 deletion EasierPath/Classes/Class/EasierLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import UIKit

public class EasierLayer:EasierLayerProtocol {
public var layer:CAShapeLayer = CAShapeLayer()

public var layer: CAShapeLayer = CAShapeLayer()
public var gradientLayer: CAGradientLayer = CAGradientLayer()
private let screenSize: CGRect = UIScreen.main.bounds
private lazy var screenWidth = screenSize.width
private lazy var screenHeight = screenSize.height

init() {
layer.fillColor = UIColor.white.cgColor
Expand All @@ -22,4 +27,12 @@ public class EasierLayer:EasierLayerProtocol {
layer.fillColor = fillColor.cgColor
return layer
}

func setGradientStyle(startPoint: CGPoint, endPoint: CGPoint, gradientColors: [CGColor]) -> CAGradientLayer {
gradientLayer.frame = CGRect(origin: .zero, size: CGSize(width: screenWidth, height: screenHeight))
gradientLayer.startPoint = startPoint
gradientLayer.endPoint = endPoint
gradientLayer.colors = gradientColors
return gradientLayer
}
}
14 changes: 10 additions & 4 deletions EasierPath/Classes/Class/EasierPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import UIKit

public class EasierPath:EasierPathProtocol {

public var lastEndPoint:CGPoint
public var path:UIBezierPath
internal var layer:CAShapeLayer = CAShapeLayer()
Expand Down Expand Up @@ -98,9 +97,16 @@ public class EasierPath:EasierPathProtocol {
}

public func makeLayer(lineWidth:CGFloat,lineColor:UIColor,fillColor:UIColor) -> CAShapeLayer {
let layer = EasierLayer()
layer.layer.path = self.path.cgPath
return layer.setStyle(lineWidth: lineWidth, lineColor: lineColor, fillColor: fillColor)
let easierLayer = EasierLayer()
easierLayer.layer.path = self.path.cgPath
return easierLayer.setStyle(lineWidth: lineWidth, lineColor: lineColor, fillColor: fillColor)
}

public func makeGradientLayer(startPoint: CGPoint, endPoint: CGPoint, gradientColors: [CGColor]) -> CAGradientLayer {
let easierLayer = EasierLayer()
easierLayer.layer.path = self.path.cgPath
easierLayer.gradientLayer.mask = easierLayer.layer
return easierLayer.setGradientStyle(startPoint: startPoint, endPoint: endPoint, gradientColors: gradientColors)
}

static internal func drawByShape(_ shape:Shape,_ rect:CGRect) -> UIBezierPath {
Expand Down
2 changes: 2 additions & 0 deletions EasierPath/Classes/Protocol/EasierLayerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ protocol EasierLayerProtocol {
//MARK: Functions

func setStyle(lineWidth:CGFloat,lineColor:UIColor,fillColor:UIColor) -> CAShapeLayer

func setGradientStyle(startPoint: CGPoint,endPoint: CGPoint,gradientColors: [CGColor]) -> CAGradientLayer
}

2 changes: 2 additions & 0 deletions EasierPath/Classes/Protocol/EasierPathProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ protocol EasierPathProtocol {

func makeLayer(lineWidth:CGFloat,lineColor:UIColor,fillColor:UIColor) -> CAShapeLayer

func makeGradientLayer(startPoint: CGPoint,endPoint: CGPoint,gradientColors: [CGColor]) -> CAGradientLayer

func addLine(_ x:CGFloat,_ y:CGFloat) -> EasierPath
}
21 changes: 17 additions & 4 deletions Example/EasierPath/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import UIKit
import EasierPath

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

setupUI()
drawBeizerPath()
drawEasierPath()
drawEasierPathShape()
drawGradientLayer()
}

func setupUI() {
Expand All @@ -43,7 +44,7 @@ class ViewController: UIViewController {
}

func drawEasierPath() {
let easierPath = EasierPath(100,300)
let easierPath = EasierPath(100,350)
.right(100)
.curve(to: .down(200), .bezier(.rightDown(50,50), .leftDown(25,150)))
.left(100)
Expand All @@ -55,9 +56,21 @@ class ViewController: UIViewController {
}

func drawEasierPathShape() {
let easierPath:EasierPath = EasierPath(.rhombus, CGRect(x: 100, y: 500, width: 100, height: 200))
let layer = easierPath.makeLayer(lineWidth:3,lineColor: .white,fillColor:.systemGreen)
let shape = EasierPath(.square, CGRect())
let layer = shape.makeLayer(lineWidth:3,lineColor: .white,fillColor:.systemGreen)

view.layer.addSublayer(layer)
}

func drawGradientLayer() {
let easierPath = EasierPath(100,600)
.right(100)
.curve(to: .down(200), .bezier(.rightDown(50,50), .leftDown(25,150)))
.left(100)
.curve(to:.up(200), .bezier(.rightUp(25,50), .leftUp(50,150)))
let gradientLayer = easierPath.makeGradientLayer(startPoint: CGPoint(x: 0.0, y: 0.5), endPoint: CGPoint(x: 1.0, y: 0.5), gradientColors: [UIColor.blue.cgColor,UIColor.red.cgColor])

view.layer.addSublayer(gradientLayer)
}
}

9 changes: 9 additions & 0 deletions Example/Tests/EasierLayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ class EasierLayerTests: XCTestCase {
XCTAssertEqual(layer.strokeColor,UIColor.black.cgColor)
XCTAssertEqual(layer.fillColor,UIColor.blue.cgColor)
}

func testEasierPath_WhenSetGradientStyle_ShouldReturnChangedGradientStyleLayer() {

let gradientLayer = sut.setGradientStyle(startPoint: CGPoint(x: 0.5, y: 1.0), endPoint: CGPoint(x: 1.0, y: 1.0), gradientColors: [UIColor.blue.cgColor,UIColor.red.cgColor])

XCTAssertEqual(gradientLayer.startPoint,CGPoint(x: 0.5, y: 1.0))
XCTAssertEqual(gradientLayer.endPoint,CGPoint(x: 1.0, y: 1.0))
XCTAssertEqual(gradientLayer.colors as! [CGColor],[UIColor.blue.cgColor,UIColor.red.cgColor])
}

}
5 changes: 5 additions & 0 deletions Example/Tests/EasierPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class EasierPathTests: XCTestCase {
override func tearDownWithError() throws {
sut = nil
}

func testEasierPath_WhenStart_ShouldReturnStartPoint() {
sut = EasierPath().start(0, 0)
XCTAssertEqual(sut.lastEndPoint,CGPoint(x: 0, y: 0))
}

func testEasierPath_WhenLeftMove_ShouldChangeLastEndPointToLeftMovePoint() {
sut = EasierPath(0,0)
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ square | circle | rectangle | oval | triangle | rhombus
:-------:|:-------:|:-------:|:-------:|:-------:|:-------:
![스크린샷 2022-06-12 오후 5 43 43](https://user-images.githubusercontent.com/47676921/173256519-0a70df62-be59-4897-8428-ff6e0d6e0aa6.png) | ![스크린샷 2022-06-12 오후 5 38 54](https://user-images.githubusercontent.com/47676921/173256447-6e1872dc-523d-46da-9d95-5ba1dd76d492.png) | ![스크린샷 2022-06-12 오후 5 39 29](https://user-images.githubusercontent.com/47676921/173256449-dce242a5-e239-4801-9e38-ff180786d277.png) | ![스크린샷 2022-06-12 오후 5 39 15](https://user-images.githubusercontent.com/47676921/173256448-991ad589-f2eb-4738-b656-16c782a96d8c.png) | ![스크린샷 2022-06-12 오후 5 39 50](https://user-images.githubusercontent.com/47676921/173256452-903e0d8c-c9b5-4ac5-a046-89b87cb62d69.png) | ![스크린샷 2022-06-12 오후 5 38 36](https://user-images.githubusercontent.com/47676921/173256446-373decb9-a51d-4338-a727-c761df70452f.png)

# Easy to fill gradient colors to UIBezierPath 🌈

```swift
let easierPath = EasierPath(100,600)
.right(100)
.curve(to: .down(200), .bezier(.rightDown(50,50), .leftDown(25,150)))
.left(100)
.curve(to:.up(200), .bezier(.rightUp(25,50), .leftUp(50,150)))
let gradientLayer = easierPath.makeGradientLayer(startPoint: CGPoint(x: 0.0, y: 0.5), endPoint: CGPoint(x: 1.0, y: 0.5), gradientColors: [UIColor.blue.cgColor,UIColor.red.cgColor])
```


<img src="https://user-images.githubusercontent.com/47676921/189566016-9829582b-e8fc-4c19-bd32-4af5318dbd04.png" width="150" height="200">


## Example
Expand All @@ -135,7 +148,7 @@ pod 'EasierPath'

## Test Coverage

> 100%
> 87.2%
## Author

Expand Down

0 comments on commit 1822b76

Please sign in to comment.