关于3D touch使用备忘

关于3D touch实现备忘

API说明和举例

首先说明的UITouch的

UIForceTouchCapability
UIForceTouchCapabilityUnknown //3D Touch检测失败
UIForceTouchCapabilityUnavailable //3D Touch不可用
UIForceTouchCapabilityAvailable //3D Touch可用

这3个枚举值就是我们来判断设备是否开启3D Touch功能,如果用户有意修改了设备的3D Touch功能,我们还有一个地方来重新检测:

  • (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection

苹果在ios9上的6s和6s支持了重压功能,具体到应用,提供给我们的api大致有二种

  • 一种是在应用logo重按,添加,这个很简单,在plist里面配置或者代码编写,都可以,具体考参考如下:
 private func setShorItem(){
        
        if #available(iOS 9.0, *){
            
            if CSDataManager.isLogin(){
                let icon1:UIApplicationShortcutIcon?
                if #available(iOS 9.1, *) {
                    icon1 = UIApplicationShortcutIcon.init(type: UIApplicationShortcutIconType.Home)
                } else {
                    // Fallback on earlier versions
                    icon1 = UIApplicationShortcutIcon.init(type: .Compose)
                }
                let item1 = UIMutableApplicationShortcutItem.init(type: "home", localizedTitle: "校园", localizedSubtitle: nil, icon:icon1, userInfo: nil)
                
                let icon2 = UIApplicationShortcutIcon.init(type: UIApplicationShortcutIconType.Share)
                let item2 = UIMutableApplicationShortcutItem.init(type: "timeline", localizedTitle: "动态", localizedSubtitle: nil, icon:icon2, userInfo: nil)
                
                let icon3:UIApplicationShortcutIcon?
                if #available(iOS 9.1, *){
                    icon3 = UIApplicationShortcutIcon.init(type: UIApplicationShortcutIconType.CaptureVideo)
                }else
                {
                    icon3 = UIApplicationShortcutIcon.init(type: UIApplicationShortcutIconType.Play)
                }
                
                let item3 = UIMutableApplicationShortcutItem.init(type: "onlineClass", localizedTitle: "在线课堂", localizedSubtitle: nil, icon:icon3, userInfo: nil)
                
                UIApplication.sharedApplication().shortcutItems = [item1,item2,item3]
            }
        }
    }

然后在具体的Appdelegate中

@available(iOS 9.0, *)
    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        
        if (shortcutItem.type == "home") {
            self.homeTabController?.selectedIndex = 0
        }else if(shortcutItem.type == "timeline"){
            self.homeTabController?.selectedIndex = 1
        }else if(shortcutItem.type == "onlineClass"){
            
            self.homeTabController?.selectedIndex = 2
            let onlineVc = LiveVedioViewController()
            let navigationVc = self.homeTabController!.selectedViewController as! UINavigationController
            navigationVc.pushViewController(onlineVc, animated: true)
        }
    }
  • 第二种是peek和pop操作,麻烦一点,但是也很好实现,在viewController中首先判断重压是否存在:
func addPeekPop(){
        
        if #available(iOS 9.0, *) {
            if self.traitCollection.forceTouchCapability == .Available && TARGET_OS_SIMULATOR != 1{
                
                self.registerForPreviewingWithDelegate(self, sourceView: self.tableView!)
                
            }
        } else {
            // Fallback on earlier versions
        }
    }

然后具体实现UIViewControllerPreviewingDelegate的协议,其中viewControllerForLocation是为了重压预览使用,当用于重压到一定程度的时候会触发回调commitViewController,即直接跳转到预览界面,具体使用代码可参考如下

extension CSTimelineViewController:UIViewControllerPreviewingDelegate{
    
    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        
        var viewController:UIViewController?
        
        if let indexPath =  self.tableView?.indexPathForRowAtPoint(location){
            
            if let cellNode = self.tableView?.nodeForRowAtIndexPath(indexPath) where cellNode.isKindOfClass(CSPostContentCellNode.self){
                
                if let post = self.posts?.posts![indexPath.section]{
                    
                    if let postImages = post.postImages{
                        
                        var previewPhotoURL = [PhotoURLModel]()
                        
                        for postImage in postImages {
                            let photoURL = PhotoURLModel()
                            photoURL.imageBigUrl = postImage.previewUrl
                            photoURL.imageSmallUrl = postImage.thumbUrl
                            previewPhotoURL.append(photoURL)
                        }
                        
                        let postCellNode = (cellNode as! CSPostContentCellNode)
                        
                        //找到对应图片的item
                        var item = 0
                        for imageNode in postCellNode.postImageNodes{
                            
                            let view = (imageNode as! ASNetworkImageNode).view
                            let point = CGPointMake(location.x, location.y - self.tableView!.contentOffset.y)
                            let childPoint = self.view.convertPoint(point, toView: view)
                            let fitView = view.hitTest(childPoint, withEvent:nil)
                            if fitView != nil{
                                item = postCellNode.postImageNodes.indexOfObject(imageNode)
                                break
                            }
                        }
                        
                        let touchIndex = NSIndexPath.init(forItem: item, inSection: 0)
                        viewController = PhotoBrowserController.getPhotoBrowser(touchIndex, photoURLs: previewPhotoURL){
                            
                            index in
                            
                            let imageNode = (cellNode as! CSPostContentCellNode).postImageNodes.objectAtIndex(touchIndex.item)
                            
                            return cellNode.view.convertRect(imageNode.frame, toView: UIApplication.sharedApplication().keyWindow!)
                            
                        }
                    }
                }
            }
        }
        
        return viewController
    }
    
    
    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
        
        let rootViewController = AppDelegate.sharedInstance().window?.rootViewController
        rootViewController?.presentViewController(viewControllerToCommit, animated: true, completion: nil)
        
    }
    

唯一需要注意的是location的坐标是相对整个sourceView的,如果sourceView是scrollView之类,计算位置点转换到具体view上需要注意点。
到具体的预览控制中可添加具体的预览动作,参考代码:

@available(iOS 9.0, *)
    override func previewActionItems() -> [UIPreviewActionItem] {
        let saveItem = UIPreviewAction.init(title: "保存照片", style: .Default, handler: {
            
            action,viewController in
            
            self.saveBtnClick()
            
            self.dimissAction()
        })
        
        return [saveItem]
    }

参考文章:

iOS9 3D Touch 实现
PeekPop

推荐阅读更多精彩内容

  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 2,733评论 8 18
  • 什么是3D Touch 3D Touch 是iOS9之后专为 iPhone6s 机型加入的新特性,这一新技术移植于...
    梁睿坤阅读 432评论 0 3
  • 1.简单的介绍一下3D Touch 3D Touch的触控技术,被苹果称为新一代多点触控技术。其实,就是此前在Ap...
    Camille_chen阅读 11,054评论 19 33
  • 专著:http://www.jianshu.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 485评论 1 0
  • 文/木棉.格 (图片来自网络) 恶 一个没有秘密的人,我多么瞧不起 寡淡得不需要任何收藏,是简单吗,苍白得多么单调...
    木棉格阅读 106评论 0 2