// // SAConfettiView.swift // Pods // // Created by Sudeep Agarwal on 12/14/15. // // import UIKit import QuartzCore public class SAConfettiView: UIView, CAAnimationDelegate { public enum ConfettiType { case Confetti case Triangle case Star case Diamond case Image(UIImage) } var emitter: CAEmitterLayer! public var colors: [UIColor]! public var intensity: Float! public var type: ConfettiType! required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } public override init(frame: CGRect) { super.init(frame: frame) setup() } func setup() { colors = [UIColor(red:0.95, green:0.40, blue:0.27, alpha:1.0), UIColor(red:1.00, green:0.78, blue:0.36, alpha:1.0), UIColor(red:0.48, green:0.78, blue:0.64, alpha:1.0), UIColor(red:0.30, green:0.76, blue:0.85, alpha:1.0), UIColor(red:0.58, green:0.39, blue:0.55, alpha:1.0)] intensity = 0.5 type = .Confetti } @objc public func startConfetti() -> Void { emitter = CAEmitterLayer() emitter.emitterPosition = CGPoint(x: self.center.x, y: 0) emitter.emitterShape = CAEmitterLayerEmitterShape.line emitter.emitterSize = CGSize(width: 40.0, height: 1) var cells = [CAEmitterCell]() for color in colors { cells.append(confettiWithColor(color: color)) } emitter.emitterCells = cells layer.addSublayer(emitter) let animation = CAKeyframeAnimation(keyPath: "birthRate") animation.duration = 1 animation.keyTimes = [0.5, 2.0] animation.values = [2.0, 0.0] animation.repeatCount = MAXFLOAT animation.delegate = self emitter.add(animation, forKey: "confettis") } override public func layoutSubviews() { super.layoutSubviews() emitter.emitterPosition = CGPoint(x: self.center.x, y: 0) } @objc public func stopConfetti() { emitter?.birthRate = 0 } func imageForType(type: ConfettiType) -> UIImage? { var fileName: String! switch type { case .Confetti: fileName = "confetti" case .Triangle: fileName = "triangle" case .Star: fileName = "star" case .Diamond: fileName = "diamond" case let .Image(customImage): return customImage } return UIImage(imageLiteralResourceName: fileName) } func confettiWithColor(color: UIColor) -> CAEmitterCell { let confetti = CAEmitterCell() confetti.birthRate = 6.0 * intensity confetti.lifetime = 14.0 * intensity confetti.lifetimeRange = 0 confetti.color = color.cgColor confetti.velocity = CGFloat(350.0 * intensity) confetti.velocityRange = CGFloat(80.0 * intensity) confetti.emissionLongitude = CGFloat(Double.pi) confetti.emissionRange = CGFloat(Double.pi / 4.0) confetti.spin = CGFloat(3.5 * intensity) confetti.spinRange = CGFloat(4.0 * intensity) confetti.scaleRange = CGFloat(intensity) confetti.scaleSpeed = CGFloat(-0.1 * intensity) confetti.contents = imageForType(type: type)!.cgImage return confetti } }