How to Implement 3D touch to use Application shortcut Items in iOS devices https://medium.com/@imrvshah/how-to-implement-3d-touch-to-use-application-shortcut-items-in-ios-devices-fe2eda3ecf3c

Peek and Pop With 3D Touch https://www.raywenderlich.com/6794-peek-and-pop-with-3d-touch

3D Touch - Peak&Pop feature. https://wysockikamil.com/3dtouch-peak-and-pop/

Swift 玩转 3D Touch 之 Peek & Pop https://www.jianshu.com/p/24be16f5fe3d

Swift开发之3DTouch实用演练 https://blog.csdn.net/shmilycoder/article/details/78286483

一、主屏幕快速操作

  1. 在Info.plist中添加 UIApplicationShortcutItems,但没找到关键字,后放弃
  2. 动态添加UIApplicationShortcutItem 在AppDelegate.swift中的didiFInishLaunchingWithOptions中添加选项添加

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

//3D Touch
let homeIcon = UIApplicationShortcutIcon(type: .compose)
let homeItem = UIApplicationShortcutItem(type: "homeAnchor", localizedTitle: "首页", localizedSubtitle: "点击进入首页", icon: homeIcon, userInfo: nil)

UIApplication.shared.shortcutItems = [homeItem]

return true

}

新增performActionFor shortcutItem方法处理选项:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { guard let tabBarVC = window?.rootViewController as? MainViewController else { return }

//根据type唯一标识进行判断跳转, 或者根据localizedTitle判断
switch shortcutItem.type {
case "homeAnchor":
    tabBarVC.selectedIndex = 1
case "play":
    let username = ShowRoomViewController()
    username.hidesBottomBarWhenPushed = true
    tabBarVC.selectedViewController?.childViewControllers.first?.present(username, animated: true, completion: nil)
case "username":
    let username = NameViewController()
    username.hidesBottomBarWhenPushed = true
    tabBarVC.selectedViewController?.childViewControllers.last?.navigationController?.pushViewController(username, animated: true)
default:
    tabBarVC.selectedIndex = 0
}

}

二、 peek and pop

对界面内某一控件的3DTouch操作 Peek和Pop是应用内的一种全新交互模式,当用户不断增加力量在控件上按压,会依次进入四个阶段 轻按控件,除触发Peek的控件外,其他区域全部虚化 继续用力Peek被触发,展示Pop界面快照 向上滑动展示快捷选项 继续用力跳转进入Pop界面

1)extend UIViewControllerPreviewingDelegate

2)add preview register in cellForItemAt override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { cell ... if #available(iOS 9.0, *) { if traitCollection.forceTouchCapability == .available { //支持3D Touch //注册Peek & Pop功能 registerForPreviewing(with: self, sourceView: cell) } }

return cell }

2)implement func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? for peek func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { //1. 获取按压的cell所在的行 guard let cell = previewingContext.sourceView as? ContentCell else { return UIViewController() } let indexPath = collectionView.indexPath(for: cell) ?? IndexPath(row: 0, section: 0)

    //2. 设定预览界面
    let vc = ContentDisplayViewController()
    // 预览区域大小(可不设置), 0为默认尺寸
    vc.preferredContentSize = CGSize(width: 0, height: 0)
    vc.postionIndex = indexPath.row
    vc.homeController = self
    vc.contentCard = cell.contentCard
    //调整不被虚化的范围,按压的那个cell不被虚化(轻轻按压时周边会被虚化,再少用力展示预览,再加力跳页至设定界面)
    let rect = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)
    //设置触发操作的视图的不被虚化的区域
    previewingContext.sourceRect = rect

    //返回预览界面
    return vc
}

3)implement func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) for pop func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { viewControllerToCommit.hidesBottomBarWhenPushed = true show(viewControllerToCommit, sender: self) }

4)extend Display controller view for actions override var previewActionItems: [UIPreviewActionItem] { let copyAction = UIPreviewAction(title: NSLocalizedString("common.copy", comment: ""), style: .default) { (action, previewViewController) in self.copyContent() }

    let confirmDelAction = UIPreviewAction(title: "common.delete".localize, style: .default) { (action, previewViewController) in
        self.delete()
    }
    let cancelDelAction = UIPreviewAction(title: "common.cancel".localize, style: .destructive) { (action, previewViewController) in
    }
    
    let delAction = UIPreviewActionGroup(title: NSLocalizedString("common.delete", comment: ""), style: .destructive, actions: [confirmDelAction, cancelDelAction])
    
    let cancelAction = UIPreviewAction(title: NSLocalizedString("common.cancel", comment: ""), style: .destructive) { (action, previewViewController) in
    }
    
    return [copyAction, delAction, cancelAction]
}