Skip to content

Commit 3ea2f0b

Browse files
committed
Updates gradientFill to use CGContext
1 parent 7b5d85d commit 3ea2f0b

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Diff for: C4/UI/Shape.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,24 @@ public class Shape: View {
9999
///An optional variable representing a gradient. If this is non-nil, then the shape will appear to be filled with a gradient.
100100
public var gradientFill: Gradient? {
101101
didSet {
102-
if let fill = gradientFill {
103-
if mask == nil {
104-
let m = Shape(self)
105-
m.fillColor = black
106-
m.strokeColor = black
107-
mask = m
108-
}
109-
fillColor = nil
110-
shapeLayer.contents = fill.render()?.cgimage
111-
} else {
112-
let image = UIImage.createWithColor(UIColor.clearColor(), size: CGSize(width: 1, height: 1)).CGImage
113-
shapeLayer.contents = image
102+
guard gradientFill != nil else {
103+
fillColor = clear
114104
return
115105
}
106+
let gim = gradientFill?.render()?.cgimage
107+
108+
//inverts coordinate for graphics context rendering
109+
var b = bounds
110+
b.origin.y = self.height - b.origin.y
116111

112+
UIGraphicsBeginImageContextWithOptions(CGSize(b.size), false, UIScreen.mainScreen().scale)
113+
let context = UIGraphicsGetCurrentContext()
114+
115+
CGContextDrawTiledImage(context, CGRect(b), gim)
116+
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
117+
let uicolor = UIColor(patternImage: uiimage)
118+
fillColor = Color(uicolor)
119+
UIGraphicsEndImageContext()
117120
}
118121
}
119122

@@ -179,9 +182,6 @@ public class Shape: View {
179182
}
180183
set(color) {
181184
shapeLayer.fillColor = color?.CGColor
182-
if color != nil {
183-
gradientFill = nil
184-
}
185185
}
186186
}
187187

0 commit comments

Comments
 (0)