3D Touch是iPhone6s之后引入的一種新的交互方式,增加了一個(gè)z軸上的操作.這樣可以減少App主要功能的操作步驟,例如微信的掃一掃和名片可以在主界面直接按壓點(diǎn)擊即可進(jìn)入.目前來(lái)說(shuō)3D Touch的使用還比較初級(jí).
快捷選項(xiàng)(Home Screen Quick Actions)
添加方式
- 通過(guò)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>
以上來(lái)自官方文檔:iOS Keys
參數(shù)詳解:
- UIApplicationShortcutItems:數(shù)組中的元素就是我們的那些快捷選項(xiàng)標(biāo)簽迂猴。
- UIApplicationShortcutItemTitle:標(biāo)簽標(biāo)題(必填)
- UIApplicationShortcutItemType:標(biāo)簽的唯一標(biāo)識(shí)(必填)
- UIApplicationShortcutItemIconType:使用系統(tǒng)圖標(biāo)的類型贪嫂,如搜索、定位、home等(可選)
- UIApplicationShortcutItemIconFile:使用項(xiàng)目中的圖片作為標(biāo)簽圖標(biāo)(可選)
- UIApplicationShortcutItemSubtitle:標(biāo)簽副標(biāo)題(可選)
- UIApplicationShortcutItemUserInfo:字典信息,如傳值使用(可選)
- 動(dòng)態(tài)添加
let shareIcon=UIApplicationShortcutIcon.init(type: .share)
let shareItem=UIApplicationShortcutItem.init(type: "com.wyz.share", localizedTitle: "分享", localizedSubtitle: "我也是分享", icon: shareIcon, userInfo: nil)
UIApplication.shared.shortcutItems=[shareItem]
點(diǎn)擊事件
我這里是簡(jiǎn)單地彈出一個(gè)alert,具體操作邏輯自行設(shè)置
//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為例
注冊(cè)
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)//注冊(cè)
}else{
print("不支持3D Touch")
}
return cell
}
回調(diào)
須創(chuàng)建一個(gè)子視圖控制器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)
}
}
創(chuàng)建預(yù)覽視圖上滑后的快捷選項(xiàng)
在TestViewController
override var previewActionItems: [UIPreviewActionItem]{
let action1=UIPreviewAction.init(title: "贊一個(gè)", style: .default) { (action, vc) in
print("贊一個(gè)")
}
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)//力度
}