zoukankan      html  css  js  c++  java
  • 3D Touch开发全面教程之Peek and Pop

    3D Touch开发全面教程之Peek and Pop - 预览和弹出


    了解3D Touch

    在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术。3D Touch的触控技术,被苹果称为新一代多点触控技术。系统只能支持iOS9+,硬件是iPhone6S+。

    iOS9提供了四类API

    1. Home Screen Quick Action : 对着Icon按压,弹出快捷菜单
    2. Peek & Pop : 对着APP内容按压,会弹出内容预览和快捷菜单
    3. WebView Peek & Pop : 使用SFSafariViewController打开的网页内容自带Peek & Pop 效果
    4. UITouch Force Properties : 检测用户按压力度

    检测是否支持3D Touch:UIForceTouchCapability 是一个枚举值,取值如下:

     case unknown		  //3D Touch检测失败
    case unavailable //3D Touch不可用
    case available  //3D Touch可用
    

    UIViewControllertraitCollection属性中,可以间接获取到UIForceTouchCapability

        if(self.traitCollection.forceTouchCapability == .available){
            //TODO ...
        }
    

    代码实现

    1. 显示peek视图
    1. peek视图时手指上滑,唤出peek视图快速选项
    代码实现 1 - 显示peek视图

    首先要控制器中注册代理

    registerForPreviewingWithDelegate(self, sourceView: view)
    

    然后实现代理UIViewControllerPreviewingDelegate 的方法func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    我们申明一个peek视图的控制器,也就是正常情况下点击UITableViewCell要跳转到的控制器,设置控制器内容和peek视图的大小,设置大小使用preferredContentSize 属性,如果为0的话则系统自动配置最佳大小

        guard let indexPath = tableView.indexPathForRow(at: location) , let cell = tableView.cellForRow(at: indexPath) else {
            return nil
        }
    
        let detailVc = DetailViewController()
    
        detailVc.preferredContentSize = CGSize( 0, height: 0)
        previewingContext.sourceRect = cell.frame
        detailVc.mTitle = cell.textLabel?.text
        return detailVc
    
    }
    
    代码实现 2 - peek视图时手指上滑,唤出peek视图快速选项

    要实现这个操作,需要在peek视图对应的控制器中重写previewActionItems方法

    比如我这里重写后是这样的:

        override var previewActionItems: [UIPreviewActionItem] {
    
        let a1 = UIPreviewAction(title: "在新标签中打开", style: .default, handler: { (action, vc) in
            print(action.title)
        })
        let a2 = UIPreviewAction(title: "加入阅读列表", style: .selected, handler: { (action, vc) in
            print(action.title)
        })
        let a31 = UIPreviewAction(title: "拷贝", style: .default, handler: { (action, vc) in
            print(action.title)
        })
        let a32 = UIPreviewAction(title: "收藏", style: .default, handler: { (action, vc) in
            print(action.title)
        })
        let a3 = UIPreviewActionGroup(title: "共享&更多...", style: .default, actions: [a31,a32])
    
        return [a1,a2,a3]
    }
    

    Web view peek and pop API (HTML链接预览功能)

    除了tableViewCell可以实现peek and pop, 原生的safari浏览器中的超链接可以支持3D touch,出现超链接的预览,使用方法和前文中提到的方法类似。

    ios 9 中增加了一个新的浏览器控制器叫做 SFSafariViewController ,它可以在你的程序中直接嵌入 Safari浏览器,简单的写一段示例代码:

    import SafariServices

            let sasfarVc = SFSafariViewController(url: URL(string:"https://www.baidu.com"), entersReaderIfAvailable: true)
        self.navigationController ?.pushViewController(sasfarVc, animated: true)
    

    这样,我们就在app内嵌的Safari浏览器并且打开了baidu的页面,并且使用3d touch超链接也会有预览的效果了。

    参考:

  • 相关阅读:
    《Real Time 3D Terrain Engines Using C++ And DirectX9》(实时地形引擎)随书源码
    (转)ogreUML类图
    (转)导弹跟踪算法
    转:正则表达式语法
    读取数据
    Python lambda用法及其与def的区别
    转Vb6.0安装失败解决办法,完全卸载VB(提高班里win7没装上VB的可以看看,我实验成功了)
    vb imagelist 作用
    二叉树的实现
    转:Python语言编程学习资料(电子书+视频教程)下载汇总:
  • 原文地址:https://www.cnblogs.com/fengtengfei/p/6443707.html
Copyright © 2011-2022 走看看