Skip to content

Commit

Permalink
clean up demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Rymarz committed Apr 23, 2018
1 parent 6a638ec commit 4d40da1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
36 changes: 25 additions & 11 deletions Demo/Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,37 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6vR-53-IsB">
<rect key="frame" x="170" y="28" width="34" height="30"/>
<state key="normal" title="Start"/>
<connections>
<action selector="startAnimation:" destination="BYZ-38-t0r" eventType="touchUpInside" id="oqu-aw-ZD9"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" verticalCompressionResistancePriority="749" image="Sample" translatesAutoresizingMaskIntoConstraints="NO" id="W93-LT-woS">
<rect key="frame" x="8" y="66" width="359" height="593"/>
<rect key="frame" x="8" y="50" width="359" height="600"/>
</imageView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="bbL-RR-J8Z">
<rect key="frame" x="0.0" y="20" width="375" height="30"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Vfm-57-d8e">
<rect key="frame" x="0.0" y="0.0" width="187.5" height="30"/>
<state key="normal" title="Break Glass"/>
<connections>
<action selector="breakGlass:" destination="BYZ-38-t0r" eventType="touchUpInside" id="BEI-GO-x1B"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1nr-Li-yaj">
<rect key="frame" x="187.5" y="0.0" width="187.5" height="30"/>
<state key="normal" title="Explode"/>
<connections>
<action selector="explode:" destination="BYZ-38-t0r" eventType="touchUpInside" id="eYC-72-ZoQ"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="W93-LT-woS" firstAttribute="top" secondItem="6vR-53-IsB" secondAttribute="bottom" constant="8" id="B5w-JG-ZdI"/>
<constraint firstItem="bbL-RR-J8Z" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="FaH-xc-yAe"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="W93-LT-woS" secondAttribute="trailing" constant="8" id="MOH-NV-eXq"/>
<constraint firstItem="6vR-53-IsB" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="W9J-1f-3ZW"/>
<constraint firstItem="6vR-53-IsB" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="8" id="g7h-A0-tJy"/>
<constraint firstItem="W93-LT-woS" firstAttribute="top" secondItem="bbL-RR-J8Z" secondAttribute="bottom" id="RwN-qd-F2P"/>
<constraint firstItem="bbL-RR-J8Z" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="m0D-eT-8PK"/>
<constraint firstItem="W93-LT-woS" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="8" id="miy-Eu-FdJ"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="bbL-RR-J8Z" secondAttribute="trailing" id="p01-pc-3Ix"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="W93-LT-woS" secondAttribute="bottom" constant="8" id="ubu-t4-gpU"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
Expand All @@ -46,6 +59,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="117.59999999999999" y="117.39130434782609"/>
</scene>
</scenes>
<resources>
Expand Down
26 changes: 16 additions & 10 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ import UIKit
class ViewController: UIViewController {
@IBOutlet weak var exampleView: UIView?

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

@IBAction func explode(_ sender: Any) {
exampleView?.explode(completion: {
self.exampleView?.alpha = 0
self.exampleView?.isHidden = false
self.reshowImage()
})
}

@IBAction func breakGlass(_ sender: Any) {
exampleView?.breakGlass(size: GridSize(columns: 15, rows: 21), completion: {
self.reshowImage()
})
}

private func reshowImage() {
self.exampleView?.alpha = 0
self.exampleView?.isHidden = false

UIView.animate(withDuration: 1, animations: {
self.exampleView?.alpha = 1
})
UIView.animate(withDuration: 1, animations: {
self.exampleView?.alpha = 1
})
}
}
10 changes: 6 additions & 4 deletions Source/Explosion/ExplosionAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public extension UIView {
/// 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 size: Describes the number of columns and rows on which the view is broken (default: 20x40)
/// - 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) {
public func explode(size: GridSize = GridSize(columns: 20, rows: 40), removeAfterCompletion: Bool = false, completion: (() -> Void)? = nil) {
guard let screenshot = self.screenshot, !isHidden else {
return
}
Expand Down Expand Up @@ -60,7 +60,10 @@ public extension UIView {
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 = CATransform3DTranslate(trans,
(-self.frame.size.width...self.frame.size.width).random() * 0.3,
(-self.frame.size.height...self.frame.size.height).random() * 0.3,
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)
Expand Down Expand Up @@ -104,7 +107,6 @@ public extension UIView {

for row in 0..<rows {
for column in 0..<columns {
// TODO: wrong scale?
let position = CGPoint(x: CGFloat(column) * singlePieceSize.width, y: CGFloat(row) * singlePieceSize.height)

let cropRect = CGRect(x: position.x * image.scale,
Expand Down

0 comments on commit 4d40da1

Please sign in to comment.