美文网首页
iOS 3DTouch功能

iOS 3DTouch功能

作者: 风儿吹啊吹 | 来源:发表于2020-07-05 20:12 被阅读0次

    1、按压应用图标显示Item列表

    1.1 代码添加item
      
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
            
            // 长按图标 显示的列表项
            let item = UIApplicationShortcutItem(type: "test.xiaopao.item", localizedTitle: "title", localizedSubtitle: "subtitle", icon: UIApplicationShortcutIcon(type: .add), userInfo: ["key" : "value"] as [String : NSSecureCoding])
            application.shortcutItems = [item]
            
            
            let window = UIWindow()
            window.rootViewController = ViewController()
            window.makeKeyAndVisible()
            self.window = window
            return true
        }
    
    1.2 点击item的 处理
        func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
            // 创建item时的 type,根据type来区分哪个item点击进来的
            let type = shortcutItem.type
            switch type {
            case "test.xiaopao.item":
                // doAction
                break
            default:
                // doAction
                break
            }
        }
    

    2、应用内 3Dtouch (Peek & Pop功能)

    概述:
    Peek和Pop是应用内的一种全新交互模式,当用户不断增加力量在控件上按压,会依次进入四个阶段:
    1、轻按控件,除触发Peek的控件外,其他区域全部虚化
    2、继续用力Peek被触发,展示Pop界面快照
    3、向上滑动展示快捷选项
    4、继续用力跳转进入Pop界面

    2.1 源控制器
    // 遵守 UIViewControllerPreviewingDelegate协议
    class ViewController: UIViewController, UIViewControllerPreviewingDelegate {
        
        // 按压要弹出的控制器
        var previewVc: PreviewViewController!
        // 按压的view
        var touchView: UIView!
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .white
            
    
            touchView = UIView()
            touchView.backgroundColor = .red
            touchView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
            view.addSubview(touchView)
    
            previewVc = PreviewViewController()
    
            // 检查页面是否属于3Dtouch
            if self.responds(to: #selector(getter: traitCollection)) {
                if self.traitCollection.forceTouchCapability == .available {
                    self.registerForPreviewing(with: self , sourceView: self.view)
                }
            }
    
        }
        
        func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
            // 按压要显示的controller
            return previewVc
        }
        
        func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
            // 按压弹出controller 之后继续按压跳转
            self.show(viewControllerToCommit, sender: self)
        }
        
    }
    
    2.2 弹出的控制器

    重写 previewActionItems 属性

    class PreviewViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            
            view.backgroundColor = .green
            preferredContentSize = CGSize(width: 300, height: 200)
            let backButton = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
            backButton.setTitle("返回", for: .normal)
            backButton.titleLabel?.textColor = .black
            backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
            view.addSubview(backButton)
            
        }
        
        @objc func backAction() {
            dismiss(animated: true, completion: nil)
            
        }
    
        override var previewActionItems: [UIPreviewActionItem] {
            return [
                UIPreviewAction(title: "title", style: .default, handler: { (action, vc) in
                    // doAction
                })
            ]
        }
    }
    
    

    参考:iOS 3DTouch功能

    相关文章

      网友评论

          本文标题:iOS 3DTouch功能

          本文链接:https://www.haomeiwen.com/subject/wlqbqktx.html