Skip to content

Commit

Permalink
add docs and rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Rymarz committed Apr 23, 2018
1 parent 79f3609 commit 6a638ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
9 changes: 8 additions & 1 deletion Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class ViewController: UIViewController {
// print("animation finished")
// })

exampleView?.explode()
exampleView?.explode(completion: {
self.exampleView?.alpha = 0
self.exampleView?.isHidden = false

UIView.animate(withDuration: 1, animations: {
self.exampleView?.alpha = 1
})
})
}
}
33 changes: 19 additions & 14 deletions Source/Explosion/ExplosionAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
import UIKit

public extension UIView {
public func explode(size: GridSize = GridSize(columns: 10, rows: 10)) {

/// Animates the view and hides it afterwards. Does nothing if view has no superview.
///
/// - Parameters:
/// - Parameter size: Describes the number of columns and rows on which the view is broken (default: 30x30)
/// - Parameter removeAfterCompletion: Removes view from superview after animation completes
/// - Parameter completion: Animation completion block
public func explode(size: GridSize = GridSize(columns: 30, rows: 30), removeAfterCompletion: Bool = false, completion: (() -> Void)? = nil) {
guard let screenshot = self.screenshot, !isHidden else {
return
}

let pieces = calculatePieces(for: screenshot, size: size)
explodeAnimation(with: pieces)
explodeAnimation(with: pieces, removeAfterCompletion: removeAfterCompletion, completion: completion)
}


// TODO: extract some methods between both animations (break glass and explosion)
private func explodeAnimation(with pieces: [ExplosionPiece]) {
private func explodeAnimation(with pieces: [ExplosionPiece], removeAfterCompletion: Bool, completion: (() -> Void)?) {
let animationView = UIView()
animationView.clipsToBounds = true
animationView.frame = self.frame
Expand All @@ -32,18 +38,18 @@ public extension UIView {
superview.addSubview(animationView)

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

private func animateExplosion(with pieces: [CALayer]) {
private func animateExplosion(with pieces: [CALayer], animationView: UIView, removeAfterCompletion: Bool, completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock {
// animationView.removeFromSuperview()
// if removeAfterCompletion {
// self.removeFromSuperview()
// }
//
// completion?()
animationView.removeFromSuperview()
if removeAfterCompletion {
self.removeFromSuperview()
}

completion?()
}
pieces.forEach { pieceLayer in
let animation = CABasicAnimation(keyPath: #keyPath(CALayer.transform))
Expand All @@ -55,7 +61,7 @@ public extension UIView {
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)
let scale: CGFloat = (0.05...0.65).random()
trans = CATransform3DScale(trans, scale, scale, 1)
animation.toValue = NSValue(caTransform3D: trans)
Expand All @@ -75,7 +81,6 @@ public extension UIView {
CATransaction.commit()
}

// TODO: extract for both animations
private func showJointPieces(_ pieces: [ExplosionPiece], animationView: UIView) -> [CALayer] {
var allPieceLayers = [CALayer]()
for index in 0..<pieces.count {
Expand Down
3 changes: 1 addition & 2 deletions Source/GlassBreak/GlassBreakAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public extension UIView {

/// Animates the view and hides it afterwards. Does nothing if view has no superview.
///
/// - Parameter size: Describes the number of columns and rows on which the view is broken
/// - Parameter size: Describes the number of columns and rows on which the view is broken (default: 10x10)
/// - Parameter removeAfterCompletion: Removes view from superview after animation completes
/// - Parameter completion: Animation completion block
public func breakGlass(size: GridSize = GridSize(columns: 10, rows: 10), removeAfterCompletion: Bool = false, completion: (() -> Void)? = nil) {
Expand All @@ -39,7 +39,6 @@ public extension UIView {
animateFalling(allPieceLayers: pieceLayers, columns: CGFloat(columns), animationView: animationView, removeAfterCompletion: removeAfterCompletion, completion: completion)
}

// TODO: create protocol and extract for both animations
private func showJointPieces(_ pieces: [[GlassPiece]], animationView: UIView) -> [CALayer] {
var allPieceLayers = [CALayer]()
for row in 0..<pieces.count {
Expand Down

0 comments on commit 6a638ec

Please sign in to comment.