Created
June 20, 2020 02:45
-
-
Save trilliwon/650a4d2cb970188ff3874597e861bffc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class KConfettiView: UIView { | |
private var emitterLayer:CAEmitterLayer! | |
var coefficient:Float = 0.5 | |
var cellCount:Int = 5 | |
var image:UIImage? | |
var color:UIColor? | |
var defaultConfettiSize:CGFloat = 3.0 | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func setup() { | |
emitterLayer = CAEmitterLayer() | |
emitterLayer.emitterPosition = CGPoint(x: self.frame.size.width / 2.0, y: 0) | |
emitterLayer.emitterShape = kCAEmitterLayerLine | |
emitterLayer.emitterSize = CGSize(width: self.frame.size.width, height: 1) | |
var cells = [CAEmitterCell]() | |
for _ in 0..<cellCount { | |
cells.append(confettiSetup(color ?? UIColor.randomColor(),image ?? UIImage.image(color: UIColor.randomColor(), size: CGSize(width: defaultConfettiSize, height: defaultConfettiSize)))) | |
} | |
emitterLayer.emitterCells = cells | |
self.layer.addSublayer(emitterLayer) | |
} | |
private func confettiSetup(_ color:UIColor,_ cimage:UIImage) -> CAEmitterCell { | |
let confetti = CAEmitterCell() | |
confetti.birthRate = 8.0 * coefficient | |
confetti.lifetime = 14.0 * coefficient | |
confetti.lifetimeRange = 0 | |
confetti.color = color.cgColor | |
confetti.velocity = CGFloat(350.0 * coefficient) | |
confetti.velocityRange = CGFloat(80.0 * coefficient) | |
confetti.emissionLongitude = .pi | |
confetti.emissionRange = .pi / 4 | |
confetti.spin = CGFloat(3.5 * coefficient) | |
confetti.spinRange = CGFloat(4.0 * coefficient) | |
confetti.scaleRange = CGFloat(coefficient) | |
confetti.scaleSpeed = CGFloat(-0.1 * coefficient) | |
confetti.contents = cimage.cgImage | |
return confetti | |
} | |
} | |
extension UIColor { | |
static func randomColor() -> Self { | |
let r = CGFloat(arc4random_uniform(255)) / 255.0 | |
let g = CGFloat(arc4random_uniform(255)) / 255.0 | |
let b = CGFloat(arc4random_uniform(255)) / 255.0 | |
return self.init(red: r, green: g, blue: b ,alpha: 1.0) | |
} | |
} | |
extension UIImage { | |
static func image(color: UIColor, size: CGSize) -> UIImage { | |
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) | |
UIGraphicsBeginImageContextWithOptions(size, false, 0) | |
color.setFill() | |
UIRectFill(rect) | |
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
return image | |
} | |
} | |
import UIKit | |
import QuartzCore | |
class Animation: UIView { | |
var caEmitter: CAEmitterLayer! | |
public var imageColors: [UIColor]! | |
public var imageName: String! | |
public var birthRate: Float! | |
required public init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
defaultSetup() | |
} | |
public override init(frame: CGRect) { | |
super.init(frame: frame) | |
defaultSetup() | |
} | |
func defaultSetup() | |
{ | |
imageColors = [ | |
UIColor(red: (255.0/255.0), green: (202.0/255), blue: (233.0/255.0), alpha: 1.0), | |
UIColor(red: (252.0/255.0), green: (61.0/255), blue: (112.0/255.0), alpha: 1.0), | |
UIColor(red: (137.0/255.0), green: (33.0/255), blue: (177.0/255.0), alpha: 1.0), | |
UIColor(red: (130.0/255.0), green: (32.0/255), blue: (74.0/255.0), alpha: 1.0), | |
UIColor(red: (64.0/255.0), green: (121.0/255), blue: (140.0/255.0), alpha: 1.0), | |
UIColor(red: (64.0/255.0), green: (121.0/255), blue: (140.0/255.0), alpha: 1.0)] | |
birthRate = 2.0 | |
} | |
func imagesWithColors(color: UIColor) -> CAEmitterCell { | |
let imageCell = CAEmitterCell() | |
imageCell.contents = UIImage.init(named: imageName)!.cgImage | |
imageCell.scale = 0.6 | |
imageCell.birthRate = birthRate | |
imageCell.lifetime = 14.0 | |
imageCell.lifetimeRange = 0 | |
imageCell.color = color.cgColor | |
imageCell.velocity = CGFloat(200.0) | |
imageCell.velocityRange = CGFloat(100.0) | |
imageCell.emissionLongitude = CGFloat(Double.pi) | |
imageCell.emissionRange = CGFloat(Double.pi/4) | |
imageCell.spin = CGFloat(2.5) | |
imageCell.spinRange = CGFloat(4.0) | |
imageCell.scaleRange = CGFloat(0.3) | |
imageCell.scaleSpeed = CGFloat(-0.1) | |
return imageCell | |
} | |
public func startAnimation() { | |
caEmitter = CAEmitterLayer() | |
caEmitter.emitterPosition = CGPoint(x: frame.size.width / 2.0, y: 0) | |
caEmitter.emitterShape = kCAEmitterLayerLine | |
caEmitter.emitterSize = CGSize(width: frame.size.width, height: 1) | |
var cells = [CAEmitterCell]() | |
for color in imageColors { | |
cells.append(imagesWithColors(color: color)) | |
} | |
caEmitter.emitterCells = cells | |
layer.addSublayer(caEmitter) | |
} | |
public func stopAnimation() { | |
caEmitter?.birthRate = 0 | |
} | |
} | |
extension UIColor { | |
open class func randomColor() -> UIColor { | |
srandom(arc4random()) | |
var red:Float = 0.0 | |
while (red < 0.1 || red > 0.84) { | |
red = Float(drand48()) | |
} | |
var green:Float = 0.0 | |
while (green < 0.1 || green > 0.84) { | |
green = Float(drand48()) | |
} | |
var blue:Float = 0.0 | |
while (blue < 0.1 || blue > 0.84) { | |
blue = Float(drand48()) | |
} | |
return UIColor.init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1.0) | |
} | |
open class func colorHash(name: String?) -> UIColor { | |
if let n = name { | |
var nameValue = 0 | |
for c in n { | |
let characterString = String(c) | |
let scalars = characterString.unicodeScalars | |
nameValue += Int(scalars[scalars.startIndex].value) | |
} | |
var r = Float((nameValue * 123) % 51) / 51.0 | |
var g = Float((nameValue * 321) % 73) / 73.0 | |
var b = Float((nameValue * 213) % 91) / 91.0 | |
r = min(max(r, 0.1), 0.84) | |
g = min(max(g, 0.1), 0.84) | |
b = min(max(b, 0.1), 0.84) | |
return UIColor.init(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: 1.0) | |
} | |
else { | |
return UIColor.red | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment