Skip to content

Shape Gradient Fill #592

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

Closed
traviskirton opened this issue Jan 14, 2016 · 3 comments
Closed

Shape Gradient Fill #592

traviskirton opened this issue Jan 14, 2016 · 3 comments

Comments

@traviskirton
Copy link
Collaborator

Here's a starting point:

override func setup() {
    let star = C4Star(center: canvas.center, pointCount: 10, innerRadius: 30, outerRadius: 60)
    canvas.add(star)

    star.lineWidth = 10.0

    let gradient = C4Gradient(frame: C4Rect(0,0,star.width,star.height), colors:[C4Pink,orange])

    let mstar = C4Star(center: gradient.center, pointCount: 10, innerRadius: 30, outerRadius: 60)

    gradient.mask = mstar
    gradient.origin = star.bounds.origin
    star.layer?.addSublayer(gradient.layer!)

    canvas.addTapGestureRecognizer { (location, state) -> () in
    }
}

Possible solution:

  • create a gradient for the shape's frame
  • create a shape for the layer's stroke
  • clear the shape layer's color / fill
  • add the fill as a sublayer
  • add the stroke layer as a sublayer

Probably best to apply a C4Gradient object, while setting nil to strokeColor, etc.

@traviskirton traviskirton changed the title Shape fill Shape Gradient Fill Jan 14, 2016
@traviskirton
Copy link
Collaborator Author

override func setup() {
    createCircle()
    createStar()
}

func createStar() {
    let s = C4Star(center: canvas.center, pointCount: 5, innerRadius: 25, outerRadius: 50)
    let m = C4Star(center: canvas.center, pointCount: 5, innerRadius: 25, outerRadius: 50)


    let g = C4Gradient(frame: s.frame, colors: [C4Pink, C4Blue])

    canvas.add(s)

    let l = g.gradientLayer

    UIGraphicsBeginImageContextWithOptions(CGSize(g.size), true, 1.0)
    let context = UIGraphicsGetCurrentContext()!
    l.renderInContext(context)
    let uiimage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();

    s.shapeLayer.contents = uiimage.CGImage!
    s.fillColor = clear
    s.mask = m

    s.lineWidth = 10.0

    canvas.addTapGestureRecognizer { (location, state) -> () in
        C4ShapeLayer.disableActions = true
        if s.shapeLayer.contents != nil {
            s.fillColor = red
            s.shapeLayer.contents = nil
        } else {
            s.shapeLayer.contents = uiimage.CGImage!
            s.fillColor = clear
        }
    }
}

func createCircle() {
    let s = C4Circle(center: canvas.center, radius: 50)
    let m = C4Circle(center: C4Point(s.width/2,s.height/2), radius: 50)

    let g = C4Gradient(frame: s.frame, colors: [C4Pink, C4Blue])

    canvas.add(s)

    let l = g.gradientLayer

    UIGraphicsBeginImageContextWithOptions(CGSize(g.size), true, 1.0)
    let context = UIGraphicsGetCurrentContext()!
    l.renderInContext(context)
    let uiimage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();

    let img = C4Image(uiimage: uiimage)
    canvas.add(img)
    s.shapeLayer.contents = uiimage.CGImage!
    s.fillColor = clear
    s.mask = m

    s.lineWidth = 10.0

    s.center = C4Point(canvas.center.x,canvas.height/3)
}

ezgif-2476733845

@traviskirton
Copy link
Collaborator Author

Sets the contents of the shape layer to an image generated from a gradient.
@Aleph7 if you think this process is okay, i'll go ahead and make it nice.

@alejandro-isaza
Copy link
Member

Looks good.

traviskirton added a commit that referenced this issue Jan 18, 2016
Includes:
- Initializer for C4Shape(shape: C4Shape)
- Initializer for C4Image() //empty image
- Initializing contents of C4Shape to a {1,1} clear image, otherwise
animating between nil and cgimageref doesn’t animate

Fixes: #592
traviskirton added a commit that referenced this issue Jan 21, 2016
Includes:
- Initializer for C4Shape(shape: C4Shape)
- Initializer for C4Image() //empty image
- Initializing contents of C4Shape to a {1,1} clear image, otherwise
animating between nil and cgimageref doesn’t animate

Fixes: #592
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants