一 使用场景
3DTouch在IOS应用中,主要有2种功能:
1 主屏幕上Icon快捷功能;
2 App内页面中的使用;
二 在APP中TableView中的使用
* 判断是否支持3DTouch
在使用之前,首先要判断当前设备是否支持3DTouch功能,不然可能造成崩溃,在使用3DTouch功能的控制器中这样判断:
if traitCollection.forceTouchCapability == .available {
registerForPreviewing(with: self, sourceView: cell)
}
* 设置代理,遵守协议,实现协议中需要实现的方法
extension TouchTableViewController: UIViewControllerPreviewingDelegate {
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let index = tableView.indexPath(for: previewingContext.sourceView as! UITableViewCell) {
let item = self.items[index.row] + "\(index.row + 1)"
let touchDetail = TouchDetailViewController()
touchDetail.item = item
return touchDetail
}
return nil
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
self.navigationController?.pushViewController(viewControllerToCommit, animated: true)
}
}
* 设置上滑预览时快捷选项
然后在第二个控制器中,可以设置预览快捷键:
override var previewActionItems: [UIPreviewActionItem] {
let action1 = UIPreviewAction(title: item, style: .default) { [weak self](_, vc) in
print(self?.item ?? "")
}
return [action1]
}
* 所有代码
//
// TouchTableViewController.swift
// Demo
//
// Created by 王云 on 2017/11/29.
// Copyright © 2017年 王云. All rights reserved.
//
import UIKit
class TouchTableViewController: UITableViewController {
var items: [String] = ["你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----", "你今天很漂亮----"]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TouchCellID")
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TouchCellID", for: indexPath)
cell.textLabel?.text = items[indexPath.row] + "\(indexPath.row + 1)"
if traitCollection.forceTouchCapability == .available {
registerForPreviewing(with: self, sourceView: cell)
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = self.items[indexPath.row] + "\(indexPath.row + 1)"
pushToTouchDetail(item: item)
}
func pushToTouchDetail(item: String) {
let touchDetail = TouchDetailViewController()
touchDetail.item = item
self.navigationController?.pushViewController(touchDetail, animated: true)
}
}
extension TouchTableViewController: UIViewControllerPreviewingDelegate {
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let index = tableView.indexPath(for: previewingContext.sourceView as! UITableViewCell) {
let item = self.items[index.row] + "\(index.row + 1)"
let touchDetail = TouchDetailViewController()
touchDetail.item = item
return touchDetail
}
return nil
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
self.navigationController?.pushViewController(viewControllerToCommit, animated: true)
}
}
在第二个控制器中:
//
// TouchDetailViewController.swift
// Demo
//
// Created by 王云 on 2017/11/29.
// Copyright © 2017年 王云. All rights reserved.
//
import UIKit
class TouchDetailViewController: UIViewController {
var item: String!
fileprivate var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "TouchDetail"
view.backgroundColor = UIColor.white
setupUI()
if let item = item {
titleLabel.text = item
}
}
func setupUI() {
titleLabel = UILabel()
titleLabel.textAlignment = .center
titleLabel.textColor = UIColor.darkGray
titleLabel.font = UIFont.systemFont(ofSize: 20)
view.addSubview(titleLabel)
titleLabel.snp.makeConstraints { (make) in
make.centerX.equalToSuperview()
make.centerY.equalToSuperview()
}
}
override var previewActionItems: [UIPreviewActionItem] {
let action1 = UIPreviewAction(title: item, style: .default) { [weak self](_, vc) in
print(self?.item ?? "")
}
return [action1]
}
}
网友评论