Skip to content

Commit

Permalink
Fix #44.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexrus committed Nov 12, 2015
1 parent 611b677 commit d6da978
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 126 deletions.
21 changes: 13 additions & 8 deletions LTMorphingLabel/LTEmitterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public struct LTEmitter {
}()

let cell: CAEmitterCell = {
let image = UIImage(named:"Sparkle")!.CGImage
let cell = CAEmitterCell()
cell.name = "sparkle"
cell.birthRate = 150.0
Expand All @@ -31,7 +30,6 @@ public struct LTEmitter {
cell.lifetimeRange = 0.1
cell.emissionLongitude = CGFloat(M_PI_2 * 2.0)
cell.emissionRange = CGFloat(M_PI_2 * 2.0)
cell.contents = image
cell.scale = 0.1
cell.yAcceleration = 100
cell.scaleSpeed = -0.06
Expand All @@ -41,9 +39,18 @@ public struct LTEmitter {

public var duration: Float = 0.6

init(name: String, duration: Float) {
init(name: String, particleName: String, duration: Float) {
cell.name = name
self.duration = duration

if let image = UIImage(
named: name,
inBundle: NSBundle(forClass: LTMorphingLabel.self),
compatibleWithTraitCollection: nil)?.CGImage {
self.cell.contents = image
} else {
cell.contents = UIImage(named: particleName)?.CGImage
}
}

public func play() {
Expand Down Expand Up @@ -86,16 +93,14 @@ public class LTEmitterView: UIView {
return _emitters
}()

public func createEmitter(name: String, duration: Float, configureClosure: LTEmitterConfigureClosure? = Optional.None) -> LTEmitter {
public func createEmitter(name: String, particleName: String, duration: Float, configureClosure: LTEmitterConfigureClosure?) -> LTEmitter {
var emitter: LTEmitter
if let e = emitterByName(name) {
emitter = e
} else {
emitter = LTEmitter(name: name, duration: duration)
emitter = LTEmitter(name: name, particleName: particleName, duration: duration)

if let closure = configureClosure {
closure(emitter.layer, emitter.cell)
}
configureClosure?(emitter.layer, emitter.cell)

layer.addSublayer(emitter.layer)
emitters.updateValue(emitter, forKey: name)
Expand Down
221 changes: 108 additions & 113 deletions LTMorphingLabel/LTMorphingLabel+Anvil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,114 +34,109 @@ extension LTMorphingLabel {
startClosures["Anvil\(LTMorphingPhaseStart)"] = {
self.emitterView.removeAllEmitters()

if self.newRects.count > 0 {
let centerRect = self.newRects[Int(self.newRects.count / 2)]

self.emitterView.createEmitter("leftSmoke", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(1 , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
layer.renderMode = kCAEmitterLayerSurface
cell.contents = UIImage(named: "Smoke")!.CGImage
cell.emissionLongitude = CGFloat(M_PI / 2.0)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 130
cell.birthRate = 60
cell.velocity = CGFloat(80 + Int(arc4random_uniform(60)))
cell.velocityRange = 100
cell.yAcceleration = -40
cell.xAcceleration = 70
cell.emissionLongitude = CGFloat(-M_PI_2)
cell.emissionRange = CGFloat(M_PI_4) / 5.0
cell.lifetime = self.morphingDuration * 2.0
cell.spin = 10
cell.alphaSpeed = -0.5 / self.morphingDuration
}

self.emitterView.createEmitter("rightSmoke", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(1 , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
layer.renderMode = kCAEmitterLayerSurface
cell.contents = UIImage(named: "Smoke")!.CGImage
cell.emissionLongitude = CGFloat(M_PI / 2.0)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 130
cell.birthRate = 60
cell.velocity = CGFloat(80 + Int(arc4random_uniform(60)))
cell.velocityRange = 100
cell.yAcceleration = -40
cell.xAcceleration = -70
cell.emissionLongitude = CGFloat(M_PI_2)
cell.emissionRange = CGFloat(-M_PI_4) / 5.0
cell.lifetime = self.morphingDuration * 2.0
cell.spin = -10
cell.alphaSpeed = -0.5 / self.morphingDuration
}

self.emitterView.createEmitter("leftFragments", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(self.font.pointSize , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
cell.contents = UIImage(named: "Fragment")!.CGImage
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 40.0
cell.color = self.textColor.CGColor
cell.birthRate = 60
cell.velocity = 350
cell.yAcceleration = 0
cell.xAcceleration = CGFloat(10 * Int(arc4random_uniform(10)))
cell.emissionLongitude = CGFloat(-M_PI_2)
cell.emissionRange = CGFloat(M_PI_4) / 5.0
cell.alphaSpeed = -2
cell.lifetime = self.morphingDuration
}

self.emitterView.createEmitter("rightFragments", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(self.font.pointSize , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
cell.contents = UIImage(named: "Fragment")!.CGImage
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 40.0
cell.color = self.textColor.CGColor
cell.birthRate = 60
cell.velocity = 350
cell.yAcceleration = 0
cell.xAcceleration = CGFloat(-10 * Int(arc4random_uniform(10)))
cell.emissionLongitude = CGFloat(M_PI_2)
cell.emissionRange = CGFloat(-M_PI_4) / 5.0
cell.alphaSpeed = -2
cell.lifetime = self.morphingDuration
}

self.emitterView.createEmitter("fragments", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(self.font.pointSize , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
cell.contents = UIImage(named: "Fragment")!.CGImage
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 40.0
cell.color = self.textColor.CGColor
cell.birthRate = 60
cell.velocity = 250
cell.velocityRange = CGFloat(Int(arc4random_uniform(20)) + 30)
cell.yAcceleration = 500
cell.emissionLongitude = 0
cell.emissionRange = CGFloat(M_PI_2)
cell.alphaSpeed = -1
cell.lifetime = self.morphingDuration
}
guard self.newRects.count > 0 else { return }

let centerRect = self.newRects[Int(self.newRects.count / 2)]

self.emitterView.createEmitter("leftSmoke", particleName: "Smoke", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(1 , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
layer.renderMode = kCAEmitterLayerSurface
cell.emissionLongitude = CGFloat(M_PI / 2.0)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 130
cell.birthRate = 60
cell.velocity = CGFloat(80 + Int(arc4random_uniform(60)))
cell.velocityRange = 100
cell.yAcceleration = -40
cell.xAcceleration = 70
cell.emissionLongitude = CGFloat(-M_PI_2)
cell.emissionRange = CGFloat(M_PI_4) / 5.0
cell.lifetime = self.morphingDuration * 2.0
cell.spin = 10
cell.alphaSpeed = -0.5 / self.morphingDuration
}

self.emitterView.createEmitter("rightSmoke", particleName: "Smoke", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(1 , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
layer.renderMode = kCAEmitterLayerSurface
cell.emissionLongitude = CGFloat(M_PI / 2.0)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 130
cell.birthRate = 60
cell.velocity = CGFloat(80 + Int(arc4random_uniform(60)))
cell.velocityRange = 100
cell.yAcceleration = -40
cell.xAcceleration = -70
cell.emissionLongitude = CGFloat(M_PI_2)
cell.emissionRange = CGFloat(-M_PI_4) / 5.0
cell.lifetime = self.morphingDuration * 2.0
cell.spin = -10
cell.alphaSpeed = -0.5 / self.morphingDuration
}

self.emitterView.createEmitter("leftFragments", particleName: "Fragment", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(self.font.pointSize , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 40.0
cell.color = self.textColor.CGColor
cell.birthRate = 60
cell.velocity = 350
cell.yAcceleration = 0
cell.xAcceleration = CGFloat(10 * Int(arc4random_uniform(10)))
cell.emissionLongitude = CGFloat(-M_PI_2)
cell.emissionRange = CGFloat(M_PI_4) / 5.0
cell.alphaSpeed = -2
cell.lifetime = self.morphingDuration
}

self.emitterView.createEmitter("rightFragments", particleName: "Fragment", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(self.font.pointSize , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 40.0
cell.color = self.textColor.CGColor
cell.birthRate = 60
cell.velocity = 350
cell.yAcceleration = 0
cell.xAcceleration = CGFloat(-10 * Int(arc4random_uniform(10)))
cell.emissionLongitude = CGFloat(M_PI_2)
cell.emissionRange = CGFloat(-M_PI_4) / 5.0
cell.alphaSpeed = -2
cell.lifetime = self.morphingDuration
}

self.emitterView.createEmitter("fragments", particleName: "Fragment", duration: 0.6) {
(layer, cell) in
layer.emitterSize = CGSizeMake(self.font.pointSize , 1)
layer.emitterPosition = CGPointMake(
centerRect.origin.x,
centerRect.origin.y + centerRect.size.height / 1.3)
cell.scale = self.font.pointSize / 90.0
cell.scaleSpeed = self.font.pointSize / 40.0
cell.color = self.textColor.CGColor
cell.birthRate = 60
cell.velocity = 250
cell.velocityRange = CGFloat(Int(arc4random_uniform(20)) + 30)
cell.yAcceleration = 500
cell.emissionLongitude = 0
cell.emissionRange = CGFloat(M_PI_2)
cell.alphaSpeed = -1
cell.lifetime = self.morphingDuration
}
}

Expand Down Expand Up @@ -180,19 +175,19 @@ extension LTMorphingLabel {

if progress > self.morphingDuration * 0.5 {
let end = self.morphingDuration * 0.55
self.emitterView.createEmitter("fragments", duration: 0.6) {_ in}.update {
self.emitterView.createEmitter("fragments", particleName: "Fragment", duration: 0.6) {_ in}.update {
(layer, cell) in
if progress > end {
layer.birthRate = 0
}
}.play()
self.emitterView.createEmitter("leftFragments", duration: 0.6) {_ in}.update {
self.emitterView.createEmitter("leftFragments", particleName: "Fragment", duration: 0.6) {_ in}.update {
(layer, cell) in
if progress > end {
layer.birthRate = 0
}
}.play()
self.emitterView.createEmitter("rightFragments", duration: 0.6) {_ in}.update {
self.emitterView.createEmitter("rightFragments", particleName: "Fragment", duration: 0.6) {_ in}.update {
(layer, cell) in
if progress > end {
layer.birthRate = 0
Expand All @@ -202,13 +197,13 @@ extension LTMorphingLabel {

if progress > self.morphingDuration * 0.63 {
let end = self.morphingDuration * 0.7
self.emitterView.createEmitter("leftSmoke", duration: 0.6) {_ in}.update {
self.emitterView.createEmitter("leftSmoke", particleName: "Smoke", duration: 0.6) {_ in}.update {
(layer, cell) in
if progress > end {
layer.birthRate = 0
}
}.play()
self.emitterView.createEmitter("rightSmoke", duration: 0.6) {_ in}.update {
self.emitterView.createEmitter("rightSmoke", particleName: "Smoke", duration: 0.6) {_ in}.update {
(layer, cell) in
if progress > end {
layer.birthRate = 0
Expand Down
6 changes: 2 additions & 4 deletions LTMorphingLabel/LTMorphingLabel+Burn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension LTMorphingLabel {
rect.origin.x + rect.size.width / 2.0,
CGFloat(progress) * rect.size.height / 1.2 + rect.origin.y)

self.emitterView.createEmitter("c\(index)", duration: self.morphingDuration) {
self.emitterView.createEmitter("c\(index)", particleName: "Fire", duration: self.morphingDuration) {
(layer, cell) in
layer.emitterSize = CGSizeMake(rect.size.width , 1)
layer.renderMode = kCAEmitterLayerAdditive
Expand All @@ -96,7 +96,6 @@ extension LTMorphingLabel {
cell.scale = self.font.pointSize / 160.0
cell.scaleSpeed = self.font.pointSize / 100.0
cell.birthRate = Float(self.font.pointSize)
cell.contents = UIImage(named: "Fire")!.CGImage
cell.emissionLongitude = CGFloat(arc4random_uniform(30))
cell.emissionRange = CGFloat(M_PI_4)
cell.alphaSpeed = self.morphingDuration * -3.0
Expand All @@ -111,7 +110,7 @@ extension LTMorphingLabel {
layer.emitterPosition = emitterPosition
}.play()

self.emitterView.createEmitter("s\(index)", duration: self.morphingDuration) {
self.emitterView.createEmitter("s\(index)", particleName: "Smoke", duration: self.morphingDuration) {
(layer, cell) in
layer.emitterSize = CGSizeMake(rect.size.width , 10)
layer.renderMode = kCAEmitterLayerAdditive
Expand All @@ -120,7 +119,6 @@ extension LTMorphingLabel {
cell.scale = self.font.pointSize / 40.0
cell.scaleSpeed = self.font.pointSize / 100.0
cell.birthRate = Float(self.font.pointSize) / Float(arc4random_uniform(10) + 10)
cell.contents = UIImage(named: "Smoke")!.CGImage
cell.emissionLongitude = 0
cell.emissionRange = CGFloat(M_PI_4)
cell.alphaSpeed = self.morphingDuration * -3
Expand Down
2 changes: 1 addition & 1 deletion LTMorphingLabel/LTMorphingLabel+Sparkle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension LTMorphingLabel {
rect.origin.x + rect.size.width / 2.0,
CGFloat(progress) * rect.size.height * 0.9 + rect.origin.y)

self.emitterView.createEmitter("c\(index)", duration: self.morphingDuration) {
self.emitterView.createEmitter("c\(index)", particleName: "Sparkle", duration: self.morphingDuration) {
(layer, cell) in
layer.emitterSize = CGSizeMake(rect.size.width , 1)
layer.renderMode = kCAEmitterLayerOutline
Expand Down
Binary file not shown.

0 comments on commit d6da978

Please sign in to comment.