Created
April 27, 2019 20:07
-
-
Save dimohamdy/0b3038b307358c4d2f238fa40b208a18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import UIKit | |
class BookCollectionViewCell: UICollectionViewCell { | |
var book: Book! | |
func configureCellWith(_ book: Book) { | |
self.book = book | |
} | |
weak var sourceVC: UIViewController! | |
weak var viewControllerPreviewing: UIViewControllerPreviewing! | |
func registerFor3DTouch(viewController: UIViewController?) { | |
if let viewController = viewController { | |
sourceVC = viewController | |
} | |
} | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
if self.traitCollection.forceTouchCapability == .available { | |
if viewControllerPreviewing != nil { | |
sourceVC.unregisterForPreviewing(withContext: viewControllerPreviewing) | |
} | |
viewControllerPreviewing = sourceVC.registerForPreviewing(with: self, sourceView: self) | |
} | |
} | |
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { | |
if self.traitCollection.forceTouchCapability == .available { | |
if viewControllerPreviewing != nil { | |
sourceVC.unregisterForPreviewing(withContext: viewControllerPreviewing) | |
} | |
viewControllerPreviewing = sourceVC.registerForPreviewing(with: self, sourceView: self) | |
} | |
} | |
} | |
extension BookCollectionViewCell: UIViewControllerPreviewingDelegate { | |
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { | |
return AboutBookViewControllerWith(book) | |
} | |
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { | |
sourceVC.show(viewControllerToCommit, sender: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment