3D Touch是iPhone6s之后引入的一种新的交互方式,增加了一个z轴上的操作.这样可以减少App主要功能的操作步骤,例如微信的扫一扫和名片可以在主界面直接按压点击即可进入.目前来说3D Touch的使用还比较初级.
快捷选项(Home Screen Quick Actions)
添加方式
- 通过plist添加
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconFile</key>
<string>open-favorites</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Favorites</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.openfavorites</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeCompose</string>
<key>UIApplicationShortcutItemTitle</key>
<string>New Message</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.newmessage</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
</dict>
</array>
以上来自官方文档:iOS Keys
参数详解:
- UIApplicationShortcutItems:数组中的元素就是我们的那些快捷选项标签。
- UIApplicationShortcutItemTitle:标签标题(必填)
- UIApplicationShortcutItemType:标签的唯一标识(必填)
- UIApplicationShortcutItemIconType:使用系统图标的类型,如搜索、定位、home等(可选)
- UIApplicationShortcutItemIconFile:使用项目中的图片作为标签图标(可选)
- UIApplicationShortcutItemSubtitle:标签副标题(可选)
- UIApplicationShortcutItemUserInfo:字典信息,如传值使用(可选)
- 动态添加
let shareIcon=UIApplicationShortcutIcon.init(type: .share)
let shareItem=UIApplicationShortcutItem.init(type: "com.wyz.share", localizedTitle: "分享", localizedSubtitle: "我也是分享", icon: shareIcon, userInfo: nil)
UIApplication.shared.shortcutItems=[shareItem]
点击事件
我这里是简单地弹出一个alert,具体操作逻辑自行设置
//AppDelegate
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let alert=UIAlertController.init(title: nil, message: shortcutItem.type, preferredStyle: .alert)
let cancelAction=UIAlertAction.init(title: "cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
window?.rootViewController?.present(alert, animated: true, completion: nil)
}
Peek&Pop
我们以TableView为例
注册
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if traitCollection.forceTouchCapability == .available {//判断是否可用
registerForPreviewing(with: self, sourceView: cell)//注册
}else{
print("不支持3D Touch")
}
return cell
}
回调
须创建一个子视图控制器TestViewController
extension ViewController: UIViewControllerPreviewingDelegate{
//peek
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
let testVc=UIStoryboard.init(name: "Test", bundle: nil).instantiateInitialViewController()
testVc?.preferredContentSize=CGSize(width: 0, height: 400)
//previewingContext.sourceRect=CGRect(x: 0, y: 0, width: view.frame.size.width, height: 44)
return testVc
}
//pop
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
show(viewControllerToCommit, sender: self)
}
}
创建预览视图上滑后的快捷选项
在TestViewController
override var previewActionItems: [UIPreviewActionItem]{
let action1=UIPreviewAction.init(title: "赞一个", style: .default) { (action, vc) in
print("赞一个")
}
let action2=UIPreviewAction.init(title: "分享一下", style: .default) { (action, vc) in
print("分享一下")
}
let action3=UIPreviewAction.init(title: "下载", style: .default) { (action, vc) in
print("下载")
}
return [action1,action2,action3]
}
获取按压力度
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch=touches.first
print(touch.force)//力度
}