3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PeekPopの3DTouchを実装する方法

Last updated at Posted at 2017-12-16

##はじめに
Peek、Popを実装した時のメモです。

##PeekとPop

###Peekとは?

弱く押し込んだ時のイベントです。

###Popとは?

Peekからさらに強く押し込んだ時のイベントです。

実装方法

1、registerForPreviewing

PeekとPopするviewをregisterForPreviewing で有効にします。

Swift
registerForPreviewing(with: self, sourceView: tableView)

2、UIViewControllerPreviewingDelegate

Peekした時にpreviewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint)が呼ばれます。
Peekで表示するViewControllerをここで返します。

Popした時にpreviewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController)が呼ばれます。
Peekしていた時のViewControllerがviewControllerToCommitになります。

Swift
extension ViewController: UIViewControllerPreviewingDelegate {
    // Peek
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        guard let indexPath = tableView.indexPathForRow(at: location) else {
            return nil
        }

        return SecondViewController(indexPath: indexPath)
    }
    
    // Pop
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        self.navigationController?.pushViewController(viewControllerToCommit, animated: true)
    }
}

##Peek quick actions

###Peek quick actionsとは?

Peekして上にスワイプすると出てくるショートカットメニューです。

実装方法

遷移後のViewControllerのpreviewActionItemsをoverrideして実装します。

Swift
    override var previewActionItems: [UIPreviewActionItem] {
        let edit = UIPreviewAction(title: "編集", style: .default) { _, _ in
            print("編集をタップ")
        }

        let delete = UIPreviewAction(title: "削除", style: .destructive) { _, _ in
            print("削除をタップ")
        }

        return [edit, delete]
    }

##GitHub
https://github.com/hideyukitone/PeekPopSample

##参考
https://qiita.com/takashings/items/ead3e9d935c2108d55a7

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?