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

Swift 5 compatibility #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 40 additions & 41 deletions Pod/Classes/SAConfettiView.swift
Expand Up @@ -10,94 +10,92 @@ import UIKit
import QuartzCore

public class SAConfettiView: UIView {

public enum ConfettiType {
case confetti
case triangle
case star
case diamond
case image(UIImage)
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!
private var active :Bool!

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)]
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
type = .Confetti
active = false
}

public func startConfetti() {
emitter = CAEmitterLayer()

emitter.emitterPosition = CGPoint(x: frame.size.width / 2.0, y: 0)
emitter.emitterShape = kCAEmitterLayerLine
emitter.emitterShape = CAEmitterLayerEmitterShape.line
emitter.emitterSize = CGSize(width: frame.size.width, height: 1)

var cells = [CAEmitterCell]()
for color in colors {
cells.append(confettiWithColor(color: color))
}

emitter.emitterCells = cells
layer.addSublayer(emitter)
active = true
}

public func stopConfetti() {
emitter?.birthRate = 0
active = false
}

func imageForType(type: ConfettiType) -> UIImage? {

var fileName: String!

switch type {
case .confetti:
case .Confetti:
fileName = "confetti"
case .triangle:
case .Triangle:
fileName = "triangle"
case .star:
case .Star:
fileName = "star"
case .diamond:
case .Diamond:
fileName = "diamond"
case let .image(customImage):
case let .Image(customImage):
return customImage
}

let path = Bundle(for: SAConfettiView.self).path(forResource: "SAConfettiView", ofType: "bundle")
let bundle = Bundle(path: path!)
let imagePath = bundle?.path(forResource: fileName, ofType: "png")
let url = URL(fileURLWithPath: imagePath!)
do {
let data = try Data(contentsOf: url)
return UIImage(data: data)
} catch {
print(error)
let url = NSURL(fileURLWithPath: imagePath!)
let data = NSData(contentsOf: url as URL)
if let data = data {
return UIImage(data: data as Data)!
}
return nil
}

func confettiWithColor(color: UIColor) -> CAEmitterCell {
let confetti = CAEmitterCell()
confetti.birthRate = 6.0 * intensity
Expand All @@ -106,17 +104,18 @@ public class SAConfettiView: UIView {
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)
confetti.emissionLongitude = CGFloat(M_PI)
confetti.emissionRange = CGFloat(M_PI_4)
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
}

public func isActive() -> Bool {
return self.active
return self.active
}
}