Skip to content

Commit

Permalink
fixed image scale and added opacity animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Rymarz committed Apr 23, 2018
1 parent 0ff2e9c commit 79f3609
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ViewController: UIViewController {
@IBOutlet weak var exampleView: UIView?

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

Expand Down
25 changes: 20 additions & 5 deletions Source/Explosion/ExplosionAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public extension UIView {
// completion?()
}
pieces.forEach { pieceLayer in
// TODO #keyPath(CALayer.transform)
let animation = CABasicAnimation(keyPath: "transform")
let animation = CABasicAnimation(keyPath: #keyPath(CALayer.transform))
animation.beginTime = CACurrentMediaTime()
animation.duration = (0.5...1.0).random()
animation.fillMode = kCAFillModeForwards
Expand All @@ -56,9 +55,22 @@ 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)
pieceLayer.add(animation, forKey: nil)

let opacityAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
opacityAnimation.beginTime = CACurrentMediaTime() + (0.01...0.3).random()
opacityAnimation.duration = (0.3...0.7).random()
opacityAnimation.fillMode = kCAFillModeForwards
opacityAnimation.isCumulative = true
opacityAnimation.isRemovedOnCompletion = false
opacityAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
opacityAnimation.toValue = 0
pieceLayer.add(opacityAnimation, forKey: nil)

}
CATransaction.commit()
}
Expand Down Expand Up @@ -90,15 +102,18 @@ public extension UIView {
// TODO: wrong scale?
let position = CGPoint(x: CGFloat(column) * singlePieceSize.width, y: CGFloat(row) * singlePieceSize.height)

let cropRect = CGRect(origin: position, size: singlePieceSize)
let cropRect = CGRect(x: position.x * image.scale,
y: position.y * image.scale,
width: singlePieceSize.width * image.scale,
height: singlePieceSize.height * image.scale)
guard
let cgImage = image.cgImage,
let pieceCgImage = cgImage.cropping(to: cropRect)
else {
continue
}

let pieceImage = UIImage(cgImage: pieceCgImage)
let pieceImage = UIImage.init(cgImage: pieceCgImage, scale: image.scale, orientation: image.imageOrientation)
let piece = ExplosionPiece(position: position, image: pieceImage)
pieces.append(piece)
}
Expand Down
5 changes: 2 additions & 3 deletions Source/GlassBreak/GlassBreakAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension UIView {
/// - Parameter size: Describes the number of columns and rows on which the view is broken
/// - Parameter removeAfterCompletion: Removes view from superview after animation completes
/// - Parameter completion: Animation completion block
public func breakAnimation(size: GridSize = GridSize(columns: 10, rows: 10), removeAfterCompletion: Bool = false, completion: (() -> Void)? = nil) {
public func breakGlass(size: GridSize = GridSize(columns: 10, rows: 10), removeAfterCompletion: Bool = false, completion: (() -> Void)? = nil) {
guard let screenshot = self.screenshot, !isHidden else {
return
}
Expand Down Expand Up @@ -67,8 +67,7 @@ public extension UIView {
completion?()
}
allPieceLayers.forEach { pieceLayer in
// TODO #keyPath(CALayer.transform)
let animation = CABasicAnimation(keyPath: "transform")
let animation = CABasicAnimation(keyPath: #keyPath(CALayer.transform))
animation.beginTime = CACurrentMediaTime() + (0.3...1.0).random()
animation.duration = (0.5...1.0).random()
animation.fillMode = kCAFillModeForwards
Expand Down

0 comments on commit 79f3609

Please sign in to comment.