Skip to content
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

Adds gradientFill to C4Shape #599

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions C4/UI/C4Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public class C4Image: C4View, NSCopying {

//MARK: Initializers

/// Initializes an empty C4Image
public override init() {
super.init()
let uiimage = UIImage()
self.view = ImageView(image: uiimage)
}

/// Initializes a new C4Image using the specified filename from the bundle (i.e. your project), it will also grab images
/// from the web if the filename starts with http.
///
Expand Down
52 changes: 50 additions & 2 deletions C4/UI/C4Shape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import UIKit

/// C4Shape is a concrete subclass of C4View that draws a bezier path in its coordinate space.
public class C4Shape: C4View {

var clearContents: CGImageRef!

internal class ShapeView : UIView {
var shapeLayer: C4ShapeLayer {
get {
Expand Down Expand Up @@ -56,6 +57,11 @@ public class C4Shape: C4View {
lineWidth = 1
lineCap = .Round
lineJoin = .Round

let i = C4Image()
i.size = C4Size(1,1)
clearContents = i.render()!.cgimage
shapeLayer.contents = clearContents
}

/// Initializest a new C4Shape from a specified C4Path.
Expand All @@ -71,7 +77,46 @@ public class C4Shape: C4View {
updatePath()
adjustToFitPath()
}


public convenience init(_ shape: C4Shape) {
self.init()
let disable = C4ShapeLayer.disableActions
C4ShapeLayer.disableActions = true
self.path = shape.path
shapeLayer.path = path?.CGPath
self.frame = shape.frame
self.lineWidth = shape.lineWidth
self.lineDashPhase = shape.lineDashPhase
self.lineCap = shape.lineCap
self.lineJoin = shape.lineJoin
self.lineDashPattern = shape.lineDashPattern
self.fillColor = shape.fillColor
self.strokeColor = shape.strokeColor
self.strokeStart = shape.strokeStart
self.strokeEnd = shape.strokeEnd
self.miterLimit = shape.miterLimit
updatePath()
adjustToFitPath()
C4ShapeLayer.disableActions = disable
}

public var gradientFill: C4Gradient? {
didSet {
guard gradientFill != nil else {
shapeLayer.contents = clearContents
return
}
if mask == nil {
let m = C4Shape(self)
m.fillColor = black
m.strokeColor = black
mask = m
}
fillColor = nil
shapeLayer.contents = gradientFill?.render()?.cgimage
}
}

/// The path defining the shape to be rendered. If the path extends outside the layer bounds it will not automatically
/// be clipped to the layer. Defaults to nil. Animatable.
public var path: C4Path? {
Expand Down Expand Up @@ -133,6 +178,9 @@ public class C4Shape: C4View {
}
set(color) {
shapeLayer.fillColor = color?.CGColor
if color != nil {
gradientFill = nil
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions C4/UI/C4ShapeLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public class C4ShapeLayer: CAShapeLayer {
animation.configureOptions()
animation.fromValue = self.lineDashPhase
return animation;
} else if key == "contents" {
let animation = CABasicAnimation(keyPath: key)
animation.configureOptions()
animation.fromValue = self.contents
return animation;
}

return super.actionForKey(key)
Expand Down