3D Touch开发全面教程之Peek and Pop - 预览和弹出

时间:2023-03-09 20:29:59
3D Touch开发全面教程之Peek and Pop - 预览和弹出

## 3D Touch开发全面教程之Peek and Pop - 预览和弹出

---

### 了解3D Touch

在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术。3D Touch的触控技术,被苹果称为新一代多点触控技术。系统只能支持iOS9+,硬件是iPhone6S+。

iOS9提供了四类API

1. Home Screen Quick Action : 对着Icon按压,弹出快捷菜单
2. Peek & Pop : 对着APP内容按压,会弹出内容预览和快捷菜单
3. WebView Peek & Pop : 使用`SFSafariViewController`打开的网页内容自带Peek & Pop 效果
4. UITouch Force Properties : 检测用户按压力度

###

检测是否支持3D Touch:`UIForceTouchCapability `是一个枚举值,取值如下:

case unknown //3D Touch检测失败
case unavailable //3D Touch不可用
case available //3D Touch可用

在`UIViewController`中`traitCollection`属性中,可以间接获取到`UIForceTouchCapability `

if(self.traitCollection.forceTouchCapability == .available){
//TODO ...
}

### 代码实现

1. 显示peek视图

2. peek视图时手指上滑,唤出peek视图快速选项

##### 代码实现 1 - 显示peek视图

首先要控制器中注册代理

registerForPreviewingWithDelegate(self, sourceView: view)

然后实现代理`UIViewControllerPreviewingDelegate `的方法`func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?`

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

我们申明一个peek视图的控制器,也就是正常情况下点击UITableViewCell要跳转到的控制器,设置控制器内容和peek视图的大小,设置大小使用preferredContentSize 属性,如果为0的话则系统自动配置最佳大小

guard let indexPath = tableView.indexPathForRow(at: location) , let cell = tableView.cellForRow(at: indexPath) else {
return nil
}

let detailVc = DetailViewController()

detailVc.preferredContentSize = CGSize(width: 0, height: 0)
previewingContext.sourceRect = cell.frame
detailVc.mTitle = cell.textLabel?.text
return detailVc

}

##### 代码实现 2 - peek视图时手指上滑,唤出peek视图快速选项

要实现这个操作,需要在peek视图对应的控制器中重写previewActionItems方法

比如我这里重写后是这样的:

override var previewActionItems: [UIPreviewActionItem] {

let a1 = UIPreviewAction(title: "在新标签中打开", style: .default, handler: { (action, vc) in
print(action.title)
})
let a2 = UIPreviewAction(title: "加入阅读列表", style: .selected, handler: { (action, vc) in
print(action.title)
})
let a31 = UIPreviewAction(title: "拷贝", style: .default, handler: { (action, vc) in
print(action.title)
})
let a32 = UIPreviewAction(title: "收藏", style: .default, handler: { (action, vc) in
print(action.title)
})
let a3 = UIPreviewActionGroup(title: "共享&更多...", style: .default, actions: [a31,a32])

return [a1,a2,a3]
}

### Web view peek and pop API (HTML链接预览功能)
除了tableViewCell可以实现peek and pop, 原生的safari浏览器中的超链接可以支持3D touch,出现超链接的预览,使用方法和前文中提到的方法类似。

ios 9 中增加了一个新的浏览器控制器叫做 ` SFSafariViewController ` ,它可以在你的程序中直接嵌入 Safari浏览器,简单的写一段示例代码:

`import SafariServices`

let sasfarVc = SFSafariViewController(url: URL(string:"https://www.baidu.com"), entersReaderIfAvailable: true)
self.navigationController ?.pushViewController(sasfarVc, animated: true)

这样,我们就在app内嵌的Safari浏览器并且打开了baidu的页面,并且使用3d touch超链接也会有预览的效果了。

参考:

- [在iOS9中使用3D Touch](http://liuyanwei.jumppo.com/2016/04/01/iOS-3DTouch-2.html)
- [iOS 3D touch开发(二) peek and pop - 预览和弹出](http://liuyanwei.jumppo.com/2016/04/01/iOS-3DTouch-2.html)