Skip to content

Instantly share code, notes, and snippets.

@calebd
Created September 17, 2015 22:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebd/e6246d416b12c52c7fa5 to your computer and use it in GitHub Desktop.
Save calebd/e6246d416b12c52c7fa5 to your computer and use it in GitHub Desktop.
//
// 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)
}
}
}
//
// 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