Skip to content

Instantly share code, notes, and snippets.

@jimrutherford
Created November 15, 2016 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimrutherford/b654af5934d4a19183ce0b66e9df841b to your computer and use it in GitHub Desktop.
Save jimrutherford/b654af5934d4a19183ce0b66e9df841b to your computer and use it in GitHub Desktop.
Confetti View
//
// Confetti.swift
// WorkRestRepeat
//
// Created by Jim Rutherford on 2015-03-28.
// Copyright (c) 2015 Taptonics. All rights reserved.
//
import UIKit
import QuartzCore
class ConfettiView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.clearColor()
setup()
}
func setup()
{
self.clipsToBounds = true
let emitterLayer = CAEmitterLayer(layer:self.layer)
emitterLayer.name = "emitterLayer"
emitterLayer.emitterPosition = CGPointMake(160, -99)
emitterLayer.emitterZPosition = 0
emitterLayer.emitterSize = CGSizeMake(320.00, 1.00)
emitterLayer.emitterDepth = 0.00
emitterLayer.emitterShape = kCAEmitterLayerLine
emitterLayer.seed = 1145243358
// Create the emitter Cell
let dotCell = CAEmitterCell()
dotCell.name = "dot"
dotCell.enabled = true
dotCell.contents = UIImage(named: "confetti-dot")?.CGImage
dotCell.contentsRect = CGRectMake(0.0, 0.0, 1.0, 1.0)
dotCell.magnificationFilter = kCAFilterLinear
dotCell.minificationFilter = kCAFilterLinear
dotCell.minificationFilterBias = 0.0
dotCell.scale = 0.15
dotCell.scaleRange = 0.0
dotCell.scaleSpeed = -0.01
dotCell.color = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).CGColor
dotCell.redRange = 1.0
dotCell.greenRange = 1.0
dotCell.blueRange = 1.0
dotCell.alphaRange = 1.0
dotCell.redSpeed = 0.0
dotCell.greenSpeed = 0.0
dotCell.blueSpeed = 0.0
dotCell.alphaSpeed = 10.0
dotCell.lifetime = 10.0
dotCell.lifetimeRange = 0.0
dotCell.birthRate = 50
dotCell.velocity = 100.0
dotCell.velocityRange = 0.0
dotCell.xAcceleration = 0.0
dotCell.yAcceleration = 0.0
dotCell.zAcceleration = 0.0
dotCell.spin = 0.0
dotCell.spinRange = 6.283
dotCell.emissionLatitude = 0.0
dotCell.emissionLongitude = 0.0
dotCell.emissionRange = 6.283
// Create the emitter Cell
let ribbonCell = CAEmitterCell()
ribbonCell.name = "ribbon"
ribbonCell.enabled = true
ribbonCell.contents = UIImage(named: "confetti-ribbon")?.CGImage
ribbonCell.contentsRect = CGRectMake(0.0, 0.0, 1.0, 1.0)
ribbonCell.magnificationFilter = kCAFilterLinear
ribbonCell.minificationFilter = kCAFilterLinear
ribbonCell.minificationFilterBias = 0.0
ribbonCell.scale = 0.35
ribbonCell.scaleRange = 0.0
ribbonCell.scaleSpeed = -0.01
ribbonCell.color = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).CGColor
ribbonCell.redRange = 1.0
ribbonCell.greenRange = 1.0
ribbonCell.blueRange = 1.0
ribbonCell.alphaRange = 1.0
ribbonCell.redSpeed = 0.0
ribbonCell.greenSpeed = 0.0
ribbonCell.blueSpeed = 0.0
ribbonCell.alphaSpeed = 10.0
ribbonCell.lifetime = 10.0
ribbonCell.lifetimeRange = 0.0
ribbonCell.birthRate = 100
ribbonCell.velocity = 100.0
ribbonCell.velocityRange = 0.0
ribbonCell.xAcceleration = 0.0
ribbonCell.yAcceleration = 0.0
ribbonCell.zAcceleration = 0.0
ribbonCell.spin = 0.0
ribbonCell.spinRange = 6.283
ribbonCell.emissionLatitude = 0.0
ribbonCell.emissionLongitude = 0.0
ribbonCell.emissionRange = 6.283
emitterLayer.emitterCells = [dotCell, ribbonCell]
self.layer.addSublayer(emitterLayer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment