Skip to content

Instantly share code, notes, and snippets.

@BrunoMiguens
Last active March 21, 2017 10:04
Show Gist options
  • Save BrunoMiguens/82e984cf24b5a29f03187ffdb87dd4b4 to your computer and use it in GitHub Desktop.
Save BrunoMiguens/82e984cf24b5a29f03187ffdb87dd4b4 to your computer and use it in GitHub Desktop.
Swift 3: 3D Touch for Peek & Pop
//
// CollectionViewController.swift
// 3D Touch
//
// Created by Bruno Miguêns on 20/03/2017.
// Copyright © 2017 Bruno Miguêns. All rights reserved.
//
// MARK: Peek & Pop: Initialization
class CollectionViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Whatever you need to do here...!
// If fource touch capability is available you'll register the 'peek & pop' delegate (UIViewControllerPreviewingDelegate).
if traitCollection.forceTouchCapability == .available {
registerForPreviewing(with: self, sourceView: view)
}
}
}
// MARK: UICollectionViewDataSource
extension CollectionViewController: UICollectionViewDataSource {
// Whatever you need to do here...!
}
// MARK: UICollectionViewDelegate
extension CollectionViewController: UICollectionViewDelegate {
// Whatever you need to do here...!
}
// MARK: UIViewControllerPreviewingDelegate
extension CollectionViewController: UIViewControllerPreviewingDelegate {
/*
Here is the place where you build the controller that will be shown to the user. You could inject some objects but you'll need to set
'preferredContentSize', with width to 0 the controller will adjust the 'peek & pop' to your screen, you will need to set the 'sourceRect'
to see the animation of the 'peek & pop' comming from the 'UICollectionViewCell'.
*/
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
let point = view.convert(location, to: collectionView)
guard let indexPath = collectionView?.indexPathForItem(at: point), let cell = collectionView?.cellForItem(at: indexPath) as? AdvertMessageCollectionViewCell else {
return nil
}
guard let detailController = storyboard?.instantiateViewController(withIdentifier: CollectionDetailViewController.ID) as? CollectionDetailViewController else {
return nil
}
detailController.preferredContentSize = CGSize(width: 0.0, height: view.height/2)
previewingContext.sourceRect = cell.frame
return detailController
}
// When you touch with more force, and if you have this code, the controller initialized before will be shown full screen to the user.
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
show(viewControllerToCommit, sender: self)
}
}
// MARK: Peek & Pop: Actions
class CollectionDetailViewController: UIViewController {
// Whatever you need to do here...!
/*
If you want to add some action to your 'peek and pop', add this overrided method to tell the 'UIViewControllerPreviewingDelegate' what
kind of actions you have available, otherwise nothing will visible.
*/
override var previewActionItems: [UIPreviewActionItem] {
let likeAction = UIPreviewAction(title: "Like", style: .default) { (action, viewController) -> Void in
print("You liked the photo")
}
let deleteAction = UIPreviewAction(title: "Delete", style: .destructive) { (action, viewController) -> Void in
print("You deleted the photo")
}
return [likeAction, deleteAction]
}
}
// Note: 'Peek & Pop' aren't available on the most of the app extensions, exception for iMessage Extension.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment