Episode #265

3D Touch Preview Interaction

15 minutes
Published on April 18, 2017

This video is only available to subscribers. Get access to this video and 584 others.

Have you ever wanted to replicate the 3D Touch actions that are available in Mail.app? How do you make these custom interactions beyond the simple action sheet that you get out of the box? In this episode Conrad walks us through adding custom interaction using 3D Touch to a list building application.

Episode Links

Provide Preview Action Items on Previewed View Controller

    override var previewActionItems: [UIPreviewActionItem] {
        get {
            let uncheck = UIPreviewAction(title: "Uncheck", style: .default) { (_, viewController : UIViewController) in
                if let vc = viewController as? OptionsViewController, let item = vc.item {
                    vc.delegate?.optionsDid(uncheck: item)
                }
            }

            let delete = UIPreviewAction(title: "Delete", style: .destructive) { (_, viewController : UIViewController) in
                if let vc = viewController as? OptionsViewController, let item = vc.item {
                    vc.delegate?.optionsDid(delete: item)
                }
            }

            return [uncheck, delete]
        }
    }

Register for Customizing Preview Interaction

    var previewInteraction : UIPreviewInteraction?

    override func viewDidLoad() {
        super.viewDidLoad()

        registerForPreviewing(with: self, sourceView: tableView)

        previewInteraction = UIPreviewInteraction(view: tableView)
        previewInteraction?.delegate = self
    }

Handling Preview Interaction Delegate Methods to Customize Previewing UI

    func previewInteraction(_ previewInteraction: UIPreviewInteraction, didUpdatePreviewTransition transitionProgress: CGFloat, ended: Bool) {
        presentedOptionsController?.updateUI(for: previewInteraction)
    }

    func previewInteractionDidCancel(_ previewInteraction: UIPreviewInteraction) {
        presentedOptionsController?.commitAction()
        presentedOptionsController = nil
    }

    optional func previewInteractionShouldBegin(_ previewInteraction: UIPreviewInteraction) -> Bool {
        return true
    }

    optional func previewInteraction(_ previewInteraction: UIPreviewInteraction, didUpdateCommitTransition transitionProgress: CGFloat, ended: Bool) {
        presentedOptionsController?.updateUI(for: previewInteraction)

        if ended {
            presentedOptionsController?.finishedPreviewing()
        }
    }

This episode uses Ios 10.2.