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

Xcode 8.3 and Swift 3.1 support #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Xcode 8.3 and Swift 3.1 support #38

wants to merge 1 commit into from

Conversation

saldous
Copy link

@saldous saldous commented Apr 3, 2017

Changed M_PI and M_PI_4 to Double.pi and Double.pi / 4 to support Xcode 8.3 and Swift 3.1 changes.

@saldous saldous changed the title Xcode 83 and Swift 3.1 support Xcode 8.3 and Swift 3.1 support Apr 3, 2017
@saldous saldous mentioned this pull request Apr 5, 2017
@saldous
Copy link
Author

saldous commented Apr 14, 2017

@sudeepag any update on when you will merge this?

@ctews
Copy link

ctews commented Apr 18, 2017

@saldous your branch has several error apparently :(

bildschirmfoto 2017-04-18 um 15 52 38

@saldous
Copy link
Author

saldous commented Apr 18, 2017

Odd, as you can see I only changed 2 lines of code!

Looks like other Swift 3.1 changes are needed.

@ctews
Copy link

ctews commented Apr 18, 2017

Would you fix those errors please?

@ctews
Copy link

ctews commented Apr 18, 2017

Don't want to hijack your effort & work :) I should've a running version here:

https://github.com/ctews/SAConfettiView

maybe your can merge in those changes into your PR branch! :)

@saldous
Copy link
Author

saldous commented Apr 18, 2017

import UIKit
import QuartzCore

open class SAConfettiView: UIView {
    
    public enum ConfettiType {
        case confetti
        case triangle
        case star
        case diamond
        case image(UIImage)
    }
    
    var emitter: CAEmitterLayer!
    open var colors: [UIColor]!
    open var intensity: Float!
    open var type: ConfettiType!
    fileprivate 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)]
        intensity = 0.5
        type = .confetti
        active = false
    }
    
    open func startConfetti() {
        emitter = CAEmitterLayer()
        
        emitter.emitterPosition = CGPoint(x: frame.size.width / 2.0, y: 0)
        emitter.emitterShape = kCAEmitterLayerLine
        emitter.emitterSize = CGSize(width: frame.size.width, height: 1)
        
        var cells = [CAEmitterCell]()
        for color in colors {
            cells.append(confettiWithColor(color))
        }
        
        emitter.emitterCells = cells
        layer.addSublayer(emitter)
        active = true
    }
    
    open func stopConfetti() {
        emitter?.birthRate = 0
        active = false
    }
    
    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
        }
        
        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!)
        let data = try? Data(contentsOf: url)
        if let data = data {
            return UIImage(data: data)!
        }
        return nil
    }
    
    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)
        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)!.cgImage
        return confetti
    }
    
    open func isActive() -> Bool {
        return self.active
    }
}

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

Successfully merging this pull request may close these issues.

None yet

2 participants