iOS 9~ Experiments - New API Components - Previewing with 3DTouch.
-
= XCode 8.0
-
= Swift 3.
-
= iOS 9.0.
-
= 3D Touch Devices.
Tested on iOS 9.0, iOS 10 Simulators iPhone 6S, 6S Plus , 7 only with forceTouchCapability Unavailable.
this is the Xcode 8 / Swift 3 updated project.
Read : UIKit Peek and Pop Protocol Reference : UIViewControllerPreviewing
To run the example project, download or clone the repo.
// Check Force touch Capability
if traitCollection.forceTouchCapability == UIForceTouchCapability.Available {
// register UIViewControllerPreviewingDelegate to enable Peek & Pop
registerForPreviewingWithDelegate(self, sourceView: view)
}else {
// 3D Touch Unavailable : present alertController or
// Provide alternatives such as touch and hold..
}
- We need to conform to UIViewControllerPreviewingDelegate
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let indexPath = tableView.indexPathForRowAtPoint(location), cell = tableView.cellForRowAtIndexPath(indexPath) else { return nil }
let vc = DetailViewController(data: sampleData[indexPath.row])
vc.preferredContentSize = CGSize(width: 0.0, height: 320.0)
previewingContext.sourceRect = cell.frame
return detailViewController
}
- Reuse an existing preview and show it
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
showViewController(viewControllerToCommit, sender: self)
}
- Build and Run!
- By pressing lightly (
Peek
) and pressing a little more firmly to actually open content (Pop
) - CAUTION! : Untested on physical hardware!
- Devices : with iPhone 6s and others 3D Touch devices!