Commit 03f5b0f0 by Sarkizz

调整扩展逻辑

parent 0d0fa55d
......@@ -12,9 +12,9 @@ extension CAGradientLayer {
public struct RenderColor {
var color: UIColor
// 颜色所在位置,0.0~1.0
var location: CGFloat
var location: Float
init(color: UIColor, location: CGFloat) {
init(color: UIColor, location: Float) {
self.color = color
self.location = location
}
......@@ -34,7 +34,11 @@ extension CAGradientLayer {
var startPoint: CGPoint {
switch self {
case .horizontal, .vertical, .adjectiveFromLeft:
case .horizontal:
return CGPoint(x: 0, y: 0.5)
case .vertical:
return CGPoint(x: 0.5, y: 0)
case .adjectiveFromLeft:
return CGPoint(x: 0, y: 0)
case .adjectiveFromRight:
return CGPoint(x: 1, y: 1)
......@@ -44,9 +48,9 @@ extension CAGradientLayer {
var endPoint: CGPoint {
switch self {
case .horizontal(let f):
return CGPoint(x: f, y: 0)
return CGPoint(x: f, y: 0.5)
case .vertical(let f):
return CGPoint(x: 0, y: f)
return CGPoint(x: 0.5, y: f)
case .adjectiveFromLeft(let f):
return CGPoint(x: f, y: f)
case .adjectiveFromRight(let f):
......@@ -58,16 +62,20 @@ extension CAGradientLayer {
extension CAGradientLayer {
public class func layer(with colors: [RenderColor], direction: RenderDirection = .vertical(1)) -> CAGradientLayer {
let renderColors = colors.map({ $0.color.cgColor })
let renderLocations = colors.map({ NSNumber(value: Float($0.location)) })
let layer = CAGradientLayer()
layer.colors = renderColors
layer.locations = renderLocations
layer.startPoint = direction.startPoint
layer.endPoint = direction.endPoint
layer.config(colors: colors, direction: direction)
return layer
}
public func config(colors: [RenderColor], direction: RenderDirection = .vertical(1)) {
let renderColors = colors.map({ $0.color.cgColor })
let renderLocations = colors.map({ NSNumber(value: $0.location) })
self.colors = renderColors
self.locations = renderLocations
self.startPoint = direction.startPoint
self.endPoint = direction.endPoint
}
}
......@@ -69,3 +69,21 @@ extension CGSize {
return CGSize(width: lhs.width * rhs.width, height: lhs.height * rhs.height)
}
}
extension CGPoint {
public static func +(lhs: CGPoint, rhs: CGFloat) -> CGPoint {
return CGPoint(x: lhs.x + rhs, y: lhs.y + rhs)
}
public static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
return CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}
public static func *(lhs: CGPoint, rhs: CGFloat) -> CGPoint {
return CGPoint(x: lhs.x * rhs, y: lhs.y * rhs)
}
public static func *(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
return CGPoint(x: lhs.x * rhs.x, y: lhs.y * rhs.y)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment