3

I'm having trouble finding enough documentation on CAEmitterCell.

It's contents seem to be able to set to any object, but as far as I can tell from examples online it's always been set to an UIImage.

Is there any way to set anything else to the contents? Like CAShapeLayers or even just solid colors?

Vadoff
  • 9,219
  • 6
  • 43
  • 39
  • I think you can draw a solid color, or any layer, covert it to image and set to EmitterCell ? Something like this http://stackoverflow.com/a/10620128/790842 – iphonic Mar 11 '15 at 07:23

1 Answers1

1

Here is one way, using paths that convert to an image (this function creates a random circle, square, or triangle):

func getRandomShape() -> UIImage {
    let rect = CGRect(x: 0, y: 0, width: 12, height: 12)
    let options = [1,2,3]
    var path:CGPath? = nil
    switch options.randomElement() {
    case 1:
        path =  CGPath(ellipseIn: rect, transform: nil)
    case 2:
        let cmp = CGMutablePath()
        cmp.addLines(between: [
            CGPoint(x: rect.midX, y: 0),
            CGPoint(x: rect.maxX, y: rect.maxY),
            CGPoint(x: rect.minX, y: rect.maxY),
            CGPoint(x: rect.midX, y: 0)
        ])
        path = cmp
    case 3:
        path = CGPath(rect: rect, transform: nil)
    default:
        path = CGPath(rect: rect, transform: nil)
    }
    
    return UIGraphicsImageRenderer(size: rect.size).image { context in
        context.cgContext.setFillColor(UIColor.white.cgColor)
        context.cgContext.addPath(path!)
        context.cgContext.fillPath()
    }
}

Don't forget to take the .cgImage of the UIImage this returns. E.g.: getRandomShape().cgImage

smakus
  • 1,107
  • 10
  • 11