3D Touch Preview Interaction

Episode #265 | 15 minutes | published on April 18, 2017 | Uses iOS-10.2
Subscribers Only
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()
        }
    }

blog comments powered by Disqus