-
-
Save calebd/e6246d416b12c52c7fa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CollectionViewController.swift | |
// Everest | |
// | |
// Created by Caleb Davenport on 6/15/15. | |
// Copyright (c) 2014-2015 Hodinkee. All rights reserved. | |
// | |
import UIKit | |
/// | |
/// A `UICollectionViewController` class that properly implements | |
/// `flashScrollIndicators` and `clearsSelectionOnViewWillAppear`. | |
/// | |
public class CollectionViewController: UICollectionViewController { | |
// MARK: - Properties | |
private var __clearsSelectionOnViewWillAppear = true | |
public override var clearsSelectionOnViewWillAppear: Bool { | |
get { | |
return false | |
} | |
set { | |
__clearsSelectionOnViewWillAppear = newValue | |
} | |
} | |
// MARK: - UIViewController | |
public override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
if __clearsSelectionOnViewWillAppear { | |
if let indexPaths = collectionView?.indexPathsForSelectedItems() as? [NSIndexPath] { | |
if let coordinator = transitionCoordinator() { | |
let animationBlock: (UIViewControllerTransitionCoordinatorContext!) -> () = { [weak self] _ in | |
self?.deselectItemsAtIndexPaths(indexPaths, animated: animated) | |
} | |
let completionBlock: (UIViewControllerTransitionCoordinatorContext!) -> () = { [weak self] context in | |
if context.isCancelled() { | |
self?.selectItemsAtIndexPaths(indexPaths, animated: false) | |
} | |
} | |
coordinator.animateAlongsideTransition(animationBlock, completion: completionBlock) | |
} | |
else { | |
deselectItemsAtIndexPaths(indexPaths, animated: animated) | |
} | |
} | |
} | |
} | |
public override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
collectionView?.flashScrollIndicators() | |
} | |
// MARK: - Private | |
private func deselectItemsAtIndexPaths(indexPaths: [NSIndexPath], animated: Bool) { | |
for indexPath in indexPaths { | |
collectionView?.deselectItemAtIndexPath(indexPath, animated: animated) | |
} | |
} | |
private func selectItemsAtIndexPaths(indexPaths: [NSIndexPath], animated: Bool) { | |
for indexPath in indexPaths { | |
collectionView?.selectItemAtIndexPath(indexPath, animated: animated, scrollPosition: .None) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TableViewController.swift | |
// Everest | |
// | |
// Created by Caleb Davenport on 9/16/15. | |
// Copyright (c) 2014-2015 Hodinkee. All rights reserved. | |
// | |
import UIKit | |
public class TableViewController: UITableViewController { | |
// MARK: - Properties | |
private var __clearsSelectionOnViewWillAppear = true | |
public override var clearsSelectionOnViewWillAppear: Bool { | |
get { | |
return false | |
} | |
set { | |
__clearsSelectionOnViewWillAppear = newValue | |
} | |
} | |
// MARK: - UIViewController | |
public override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
if __clearsSelectionOnViewWillAppear { | |
if let indexPaths = tableView?.indexPathsForSelectedRows() as? [NSIndexPath] { | |
if let coordinator = transitionCoordinator() { | |
let animationBlock: (UIViewControllerTransitionCoordinatorContext!) -> () = { [weak self] _ in | |
self?.deselectItemsAtIndexPaths(indexPaths, animated: animated) | |
} | |
let completionBlock: (UIViewControllerTransitionCoordinatorContext!) -> () = { [weak self] context in | |
if context.isCancelled() { | |
self?.selectItemsAtIndexPaths(indexPaths, animated: false) | |
} | |
} | |
coordinator.animateAlongsideTransition(animationBlock, completion: completionBlock) | |
} | |
else { | |
deselectItemsAtIndexPaths(indexPaths, animated: animated) | |
} | |
} | |
} | |
} | |
// MARK: - Private | |
private func deselectItemsAtIndexPaths(indexPaths: [NSIndexPath], animated: Bool) { | |
for indexPath in indexPaths { | |
tableView?.deselectRowAtIndexPath(indexPath, animated: animated) | |
} | |
} | |
private func selectItemsAtIndexPaths(indexPaths: [NSIndexPath], animated: Bool) { | |
for indexPath in indexPaths { | |
tableView?.selectRowAtIndexPath(indexPath, animated: animated, scrollPosition: .None) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment