iOS 开发

关于3D touch使用备忘

2016-05-10  本文已影响116人  青花瓷的平方

关于3D touch实现备忘

API说明和举例

首先说明的UITouch的

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

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

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

 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)
        }
    }
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

上一篇下一篇

猜你喜欢

热点阅读