Skip to content

Commit

Permalink
adds example animation for UICollectionView. marcosgriselli#32
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgriselli committed Feb 27, 2018
1 parent 334ee3d commit 7fb4da4
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Example/ViewAnimator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
85B07AB8CAAAAA417A4A888D /* Pods_ViewAnimator_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA78A58AE09B3C6EAB2E0F5 /* Pods_ViewAnimator_Example.framework */; };
FB7FAA701F8BDD2B00B46571 /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB7FAA6F1F8BDD2B00B46571 /* CollectionViewCell.swift */; };
FB7FAA721F8BE01E00B46571 /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB7FAA711F8BE01D00B46571 /* TableViewCell.swift */; };
FBB74CD520277D8E007DA94F /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBB74CD420277D8E007DA94F /* CollectionViewController.swift */; };
FBD2D4A9200C1EB800C3785B /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBD2D4A8200C1EB800C3785B /* TableViewController.swift */; };
FBEDC6621F8B1EEA00BC2A49 /* Designable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBEDC6611F8B1EEA00BC2A49 /* Designable.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -53,6 +54,7 @@
D51DE1497D6B8B24A16A1617 /* Pods_ViewAnimator_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ViewAnimator_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FB7FAA6F1F8BDD2B00B46571 /* CollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = "<group>"; };
FB7FAA711F8BE01D00B46571 /* TableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewCell.swift; sourceTree = "<group>"; };
FBB74CD420277D8E007DA94F /* CollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = "<group>"; };
FBD2D4A8200C1EB800C3785B /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
FBEDC6611F8B1EEA00BC2A49 /* Designable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Designable.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -199,6 +201,7 @@
children = (
607FACD71AFB9204008FA782 /* ViewController.swift */,
FBD2D4A8200C1EB800C3785B /* TableViewController.swift */,
FBB74CD420277D8E007DA94F /* CollectionViewController.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -420,6 +423,7 @@
FBD2D4A9200C1EB800C3785B /* TableViewController.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
FBB74CD520277D8E007DA94F /* CollectionViewController.swift in Sources */,
FB7FAA701F8BDD2B00B46571 /* CollectionViewCell.swift in Sources */,
FBEDC6621F8B1EEA00BC2A49 /* Designable.swift in Sources */,
);
Expand Down
95 changes: 93 additions & 2 deletions Example/ViewAnimator/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<connections>
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="viewControllers" id="dEP-m2-NkK"/>
<segue destination="y9f-iQ-3Do" kind="relationship" relationship="viewControllers" id="2NJ-V0-bGw"/>
<segue destination="d6M-ta-yMh" kind="relationship" relationship="viewControllers" id="vzR-dg-agj"/>
</connections>
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="2Fg-gg-Ro8" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down Expand Up @@ -108,7 +109,7 @@
<rect key="frame" x="0.0" y="28" width="375" height="60"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="vMW-1T-s5f" id="0mw-bg-mOo">
<rect key="frame" x="0.0" y="0.0" width="342" height="59.5"/>
<rect key="frame" x="0.0" y="0.0" width="342" height="59.666666666666664"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="zNY-Kw-AI8">
Expand Down Expand Up @@ -216,7 +217,7 @@
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="nAu-sW-RHj" id="2ZG-wa-Xsa">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="43.666666666666664"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
Expand Down Expand Up @@ -245,6 +246,77 @@
</objects>
<point key="canvasLocation" x="-396" y="1535"/>
</scene>
<!--CollectionView-->
<scene sceneID="IWO-55-e04">
<objects>
<viewController id="r2d-tc-8fj" customClass="CollectionViewController" customModule="ViewAnimator_Example" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Jm5-65-rMu"/>
<viewControllerLayoutGuide type="bottom" id="DDr-wK-DCH"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="fmr-pm-yEy">
<rect key="frame" x="0.0" y="0.0" width="375" height="812"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="hSy-5M-nOm">
<rect key="frame" x="0.0" y="140" width="375" height="545"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="5" minimumInteritemSpacing="5" id="NAm-dQ-LV0">
<size key="itemSize" width="177" height="253"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="Mz3-rw-Hfm">
<rect key="frame" x="0.0" y="0.0" width="177" height="253"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO">
<rect key="frame" x="0.0" y="0.0" width="177" height="253"/>
<autoresizingMask key="autoresizingMask"/>
</view>
</collectionViewCell>
</cells>
</collectionView>
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" animating="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="tnU-O2-2YK">
<rect key="frame" x="177" y="396" width="20" height="20"/>
</activityIndicatorView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="tnU-O2-2YK" firstAttribute="centerX" secondItem="fmr-pm-yEy" secondAttribute="centerX" id="4FA-9X-Ojd"/>
<constraint firstItem="tnU-O2-2YK" firstAttribute="centerY" secondItem="fmr-pm-yEy" secondAttribute="centerY" id="Qc8-c2-5cW"/>
<constraint firstItem="hSy-5M-nOm" firstAttribute="leading" secondItem="fmr-pm-yEy" secondAttribute="leading" id="TLh-9e-7yT"/>
<constraint firstAttribute="trailing" secondItem="hSy-5M-nOm" secondAttribute="trailing" id="Xfz-OH-aNl"/>
<constraint firstItem="DDr-wK-DCH" firstAttribute="top" secondItem="hSy-5M-nOm" secondAttribute="bottom" id="fzy-O1-Vb2"/>
<constraint firstItem="hSy-5M-nOm" firstAttribute="top" secondItem="Jm5-65-rMu" secondAttribute="bottom" id="mlM-OQ-J0S"/>
</constraints>
</view>
<toolbarItems/>
<navigationItem key="navigationItem" title="CollectionView" id="IQe-c4-g6Z">
<rightBarButtonItems>
<barButtonItem title="Reset" id="c10-I5-VZI">
<connections>
<action selector="resetTapped:" destination="r2d-tc-8fj" id="Xej-Tf-kfr"/>
</connections>
</barButtonItem>
<barButtonItem title="Animate" id="OW1-9v-33r">
<connections>
<action selector="animateTapped:" destination="r2d-tc-8fj" id="fU0-W5-P3h"/>
</connections>
</barButtonItem>
</rightBarButtonItems>
</navigationItem>
<simulatedToolbarMetrics key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="activityIndicator" destination="tnU-O2-2YK" id="V47-q5-4qN"/>
<outlet property="collectionView" destination="hSy-5M-nOm" id="wcZ-kZ-iwo"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="vuC-4N-sDe" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="487" y="1535"/>
</scene>
<!--TableView-->
<scene sceneID="cV2-LB-XaS">
<objects>
Expand All @@ -264,5 +336,24 @@
</objects>
<point key="canvasLocation" x="-395.19999999999999" y="770.68965517241384"/>
</scene>
<!--CollectionView-->
<scene sceneID="oUW-63-zxC">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="d6M-ta-yMh" sceneMemberID="viewController">
<tabBarItem key="tabBarItem" title="CollectionView" id="lgO-xL-d8H"/>
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" largeTitles="YES" id="HRN-Xq-Z6D">
<rect key="frame" x="0.0" y="44" width="375" height="96"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="r2d-tc-8fj" kind="relationship" relationship="rootViewController" id="jY9-1o-SUw"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="bxO-Cw-Hx3" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="463" y="771"/>
</scene>
</scenes>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// CollectionViewController.swift
// ViewAnimator_Example
//
// Created by Marcos Griselli on 04/02/2018.
// Copyright © 2018 CocoaPods. All rights reserved.
//

import UIKit
import ViewAnimator

private let reuseIdentifier = "Cell"

class CollectionViewController: UIViewController {

private var items = [Any?]()
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
private let animations = [AnimationType.from(direction: .bottom, offset: 30.0)]

override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}

@IBAction func animateTapped(_ sender: UIBarButtonItem) {
sender.isEnabled = false
activityIndicator.stopAnimating()
items = Array(repeating: nil, count: 20)
collectionView?.reloadData()
collectionView?.performBatchUpdates({
self.collectionView?.animateViews(animations: animations,
completion: {
sender.isEnabled = true
})
}, completion: nil)
}

@IBAction func resetTapped(_ sender: UIBarButtonItem) {
items.removeAll()
collectionView?.animateViews(animations: animations, reversed: true, initialAlpha: 1.0, finalAlpha: 0.0,
completion: {
self.collectionView?.restoreViewsToIdentity()
self.collectionView?.reloadData()
self.activityIndicator.startAnimating()
})
}
}

// MARK: UICollectionViewDataSource
extension CollectionViewController: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.contentView.backgroundColor = UIColor.red
return cell
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TableViewController: UITableViewController {

private var items = [Any?]()
private let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
private let animations = [AnimationType.from(direction: .bottom, offset: 30.0)]

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -47,16 +48,14 @@ class TableViewController: UITableViewController {
activityIndicator.stopAnimating()
items = Array(repeating: nil, count: 20)
tableView.reloadData()
tableView.animateViews(animations: [AnimationType.from(direction: .bottom, offset: 30.0)],
completion: {
tableView.animateViews(animations: animations, completion: {
sender.isEnabled = true
})
}

@IBAction func resetTapped(_ sender: UIBarButtonItem) {
items.removeAll()
let animation = AnimationType.from(direction: .bottom, offset: 30.0)
tableView.animateViews(animations: [animation], reversed: true, initialAlpha: 1.0, finalAlpha: 0.0,
tableView.animateViews(animations: animations, reversed: true, initialAlpha: 1.0, finalAlpha: 0.0,
completion: {
self.tableView.restoreViewsToIdentity()
self.tableView.reloadData()
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gem "fastlane"
gem "cocoapods"

2 changes: 1 addition & 1 deletion ViewAnimator/Classes/Animatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public extension Animatable {

prepareViews(initialAlpha: initialAlpha)
let dispatchGroup = DispatchGroup()
for _ in 1...self.views.count { dispatchGroup.enter() }
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
for _ in 1...self.views.count { dispatchGroup.enter() }
for (index, view) in self.views.enumerated() {
view.alpha = initialAlpha
view.animate(animations: animations,
Expand Down
66 changes: 66 additions & 0 deletions ViewAnimator/Classes/Utilities/Random.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Utilities.swift
// Pods-ViewAnimator_Example
//
// Created by Marcos Griselli on 10/7/17.
//

import Foundation

// MARK: - Bool
extension Bool {

/// Generates a random bool.
///
/// - Returns: Bool.
static func random() -> Bool {
return arc4random_uniform(2) == 0
}
}

// MARK: Double
public extension Double {

/// Returns a random floating point number between 0.0 and 1.0, inclusive.
public static var random: Double {
return Double(arc4random()) / 0xFFFFFFFF
}

/// Generates a random double.
///
/// - Parameters:
/// - min: Minimum value of the random value.
/// - max: Maximum value of the random value.
/// - Returns: Generated value.
public static func random(min: Double, max: Double) -> Double {
return Double.random * (max - min) + min
}
}

// MARK: Float Extension

public extension Float {

/// Returns a random floating point number between 0.0 and 1.0, inclusive.
public static var random: Float {
return Float(arc4random()) / 0xFFFFFFFF
}
}

extension CGFloat {

/// Returns a random floating point number between 0.0 and 1.0, inclusive.
public static var random: CGFloat {
return CGFloat(Float.random)
}

/// Generates a random CGFloat.
///
/// - Parameters:
/// - min: Minimum value of the random value.
/// - max: Maximum value of the random value.
/// - Returns: Generated value.
public static func random(min: CGFloat, max: CGFloat) -> CGFloat {
return CGFloat.random * (max - min) + min
}
}
16 changes: 16 additions & 0 deletions ViewAnimator/Classes/Utilities/UIScrollView + isRefreshing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// UIScrollView + isRefreshing.swift
// ViewAnimator
//
// Created by Marcos Griselli on 1/24/18.
//

import UIKit

@available(iOS 10.0, *)
extension UIScrollView {

var isRefreshControlVisible: Bool {
return refreshControl?.isRefreshing == true
}
}

0 comments on commit 7fb4da4

Please sign in to comment.