Skip to content

Commit

Permalink
explosion animation
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-ios-dev committed Apr 22, 2018
1 parent a7a7667 commit 0ff2e9c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class ViewController: UIViewController {
@IBOutlet weak var exampleView: UIView?

@IBAction func startAnimation(_ sender: Any) {
exampleView?.breakAnimation(size: GridSize(columns: 15, rows: 21), removeAfterCompletion: true, completion: {
print("animation finished")
})
// exampleView?.breakAnimation(size: GridSize(columns: 15, rows: 21), removeAfterCompletion: true, completion: {
// print("animation finished")
// })

exampleView?.explode()
}
}
29 changes: 29 additions & 0 deletions Source/Explosion/ExplosionAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ public extension UIView {
superview.addSubview(animationView)

let pieceLayers = showJointPieces(pieces, animationView: animationView)
animateExplosion(with: pieceLayers)
}

private func animateExplosion(with pieces: [CALayer]) {
CATransaction.begin()
CATransaction.setCompletionBlock {
// animationView.removeFromSuperview()
// if removeAfterCompletion {
// self.removeFromSuperview()
// }
//
// completion?()
}
pieces.forEach { pieceLayer in
// TODO #keyPath(CALayer.transform)
let animation = CABasicAnimation(keyPath: "transform")
animation.beginTime = CACurrentMediaTime()
animation.duration = (0.5...1.0).random()
animation.fillMode = kCAFillModeForwards
animation.isCumulative = true
animation.isRemovedOnCompletion = false
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
var trans = pieceLayer.transform
trans = CATransform3DTranslate(trans, (-15.0...15.0).random(), (0...self.frame.size.height).random() * -0.65, 0)
// trans = CATransform3DRotate(trans, (-1.0...1.0).random() * CGFloat.pi * 0.25, 0, 0, 1)
animation.toValue = NSValue(caTransform3D: trans)
pieceLayer.add(animation, forKey: nil)
}
CATransaction.commit()
}

// TODO: extract for both animations
Expand Down
1 change: 1 addition & 0 deletions Source/GlassBreak/GlassBreakAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public extension UIView {
completion?()
}
allPieceLayers.forEach { pieceLayer in
// TODO #keyPath(CALayer.transform)
let animation = CABasicAnimation(keyPath: "transform")
animation.beginTime = CACurrentMediaTime() + (0.3...1.0).random()
animation.duration = (0.5...1.0).random()
Expand Down

0 comments on commit 0ff2e9c

Please sign in to comment.