Skip to content

Updates gradientFill to use CGContext #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions C4/UI/Shape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,24 @@ public class Shape: View {
///An optional variable representing a gradient. If this is non-nil, then the shape will appear to be filled with a gradient.
public var gradientFill: Gradient? {
didSet {
if let fill = gradientFill {
if mask == nil {
let m = Shape(self)
m.fillColor = black
m.strokeColor = black
mask = m
}
fillColor = nil
shapeLayer.contents = fill.render()?.cgimage
} else {
let image = UIImage.createWithColor(UIColor.clearColor(), size: CGSize(width: 1, height: 1)).CGImage
shapeLayer.contents = image
guard gradientFill != nil else {
fillColor = clear
return
}
let gim = gradientFill?.render()?.cgimage

//inverts coordinate for graphics context rendering
var b = bounds
b.origin.y = self.height - b.origin.y

UIGraphicsBeginImageContextWithOptions(CGSize(b.size), false, UIScreen.mainScreen().scale)
let context = UIGraphicsGetCurrentContext()

CGContextDrawTiledImage(context, CGRect(b), gim)
let uiimage = UIGraphicsGetImageFromCurrentImageContext()
let uicolor = UIColor(patternImage: uiimage)
fillColor = Color(uicolor)
UIGraphicsEndImageContext()
}
}

Expand Down Expand Up @@ -179,9 +182,6 @@ public class Shape: View {
}
set(color) {
shapeLayer.fillColor = color?.CGColor
if color != nil {
gradientFill = nil
}
}
}

Expand Down