简单介绍一下iOS中的3Dtouch. 一般开发中主要有三种情境使用:
icon的3Dtouch
应用内的预览
再就是可能游戏中会用到的按压力度的检测了

icon的3D touch

第一种方式: 在info.plist中添加

3Dtouch with info.plist

效果如下

解释一下其中的一些key的意思:
UIApplicationShortcutItemIconType 是使用系统提供的图标, 目前提供了近30种图标
UIApplicationShortcutItemIconFile 是 .xcassets 文件中自定义的图标
UIApplicationShortcutItemType 是应用通过3Dtouch启动时传给应用内部的表示, 让内部代码知道点击的是哪个item.
其他的key显而易见, 不做解释.

那么接下来就是在代码中处理3Dtouch事件了:
1.首先应用第一次启动:

1
2
3
4
5
6
7
8
var launchedShortcutItem: UIApplicationShortcutItem?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] {
launchedShortcutItem = shortcutItem as? UIApplicationShortcutItem
}
return true
}

2.应用已经启动, 并从后台3Dtouch启动:

1
2
3
4
5
6
7
func application(_ application: UIApplication,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void) {
let handleAction = handleShortcutItem(shortcutItem)
completionHandler(handleAction)
}

通过以上两种方式都获取了shortcutItem的信息, 就可以进行相应的逻辑处理了.

用代码动态添加shortcutItem

1
2
3
4
5
6
7
let item = UIApplicationShortcutItem(type: "two",
localizedTitle: "Phoenix",
localizedSubtitle: "Make a Call",
icon: UIApplicationShortcutIcon(type: .cloud),
userInfo: nil)
UIApplication.shared.shortcutItems = [item]

注意: 通过info.plist 和 代码添加的shortcutItem 并不冲突, 取并集, 但系统限制最多能够显示4个item, 因此会自动按顺序截取前4个.

应用内预览

先看一下效果图

peek & pop

peek是由一个能响应事件的view触发的, 需要在viewDidLoad中注册代理和来源视图:

1
2
3
4
override func viewDidLoad() {
super.viewDidLoad()
self.registerForPreviewing(with: self as UIViewControllerPreviewingDelegate, sourceView: self.view)
}

然后是遵守协议和实现协议方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
// 返回目标控制器
let indexPath = self.tableView.indexPathForRow(at: location)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let content = storyboard.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
guard indexPath != nil else {
return nil
}
return content
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
self.showDetailViewController(viewControllerToCommit, sender: self)
}

peek quick actions

在目标控制器实现以下方法:

1
2
3
4
5
6
override var previewActionItems: [UIPreviewActionItem] {
let action = UIPreviewAction(title: "Save", style: .default) { (action: UIPreviewAction, controller: UIViewController) in
print("Save image")
}
return [action]
}

按压力度监听

实现了一个小demo


代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
self.centerPoint = touch?.location(in: touch?.view)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let force = touch?.force
let maximumPossibleForce = touch?.maximumPossibleForce
let quotient = force! / maximumPossibleForce!
let border = self.view.frame.size.width
self.round.layer.cornerRadius = border * 0.5 * quotient
self.round.frame = CGRect(x: 0, y: 0, width: border * quotient, height: border * quotient)
let point = touch?.location(in: touch?.view)
self.round.center = point!
let weight = 415 * quotient
self.weightLabel.text = "\(weight) g"
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.round.frame = CGRect.zero
self.weightLabel.text = "0 g"
}

本文完整demo (3Dtouch不支持模拟器, 请以真机调试)

__原创文章, 转载请注明出处