IOS 3Dtouch

本文介绍如何为iOS应用IT江湖添加3DTouch功能,包括使用SBShortcutMenuSimulator进行测试的方法、实现icon重按弹出菜单及tableViewCell预览文章详情的功能,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


https://github.com/itjhDev/itjh_3DTouch



昨天闲来无事,对着自己的iPhone6s,准备给IT江湖加入3D Touch吧!!

没有6s的 模拟器也可以测试,按照@conradev 的SBShortcutMenuSimulator测试

项目地址:https://github.com/DeskConnect/SBShortcutMenuSimulator.git

  • icon 重按弹出菜单
  • tableView Cell 轻按按预览文章详情
  • tableView Cell 轻按按预览文章详情,上滑底部显示Action
  • tableView Cell 轻按按预览文章详情,重按进入文章详情页

效果图

  

完整的3D Touch 动画视频

icon 重按弹出菜单

代码片段:

AppleDelegate文件中

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        //添加icon 3d Touch
        let firstItemIcon:UIApplicationShortcutIcon = UIApplicationShortcutIcon(type: .Share)
        let firstItem = UIMutableApplicationShortcutItem(type: "1", localizedTitle: "分享", localizedSubtitle: nil, icon: firstItemIcon, userInfo: nil)

        let firstItemIcon1:UIApplicationShortcutIcon = UIApplicationShortcutIcon(type: .Compose)
        let firstItem1 = UIMutableApplicationShortcutItem(type: "2", localizedTitle: "编辑", localizedSubtitle: nil, icon: firstItemIcon1, userInfo: nil)


        application.shortcutItems = [firstItem,firstItem1]

        return true
    }

    /**
    3D Touch 跳转

    - parameter application:       application
    - parameter shortcutItem:      item
    - parameter completionHandler: handler
    */
    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

        let handledShortCutItem = handleShortCutItem(shortcutItem)
        completionHandler(handledShortCutItem)

    }

    func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
        var handled = false

        if shortcutItem.type == "1" { //分享

            let rootNavigationViewController = window!.rootViewController as? UINavigationController
            let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?

            rootNavigationViewController?.popToRootViewControllerAnimated(false)
            rootViewController?.performSegueWithIdentifier("toShare", sender: nil)
            handled = true

        }

        if shortcutItem.type == "2" { //编辑

            let rootNavigationViewController = window!.rootViewController as? UINavigationController
            let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?

            rootNavigationViewController?.popToRootViewControllerAnimated(false)
            rootViewController?.performSegueWithIdentifier("toCompose", sender: nil)
            handled = true

        }
        return handled
    }

tableView Cell 轻按按预览文章详情

代码片段:

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


        let cellPosition = tableView.convertPoint(location, fromView: view)

        if let touchedIndexPath = tableView.indexPathForRowAtPoint(cellPosition) {

            tableView.deselectRowAtIndexPath(touchedIndexPath, animated: true)

            let aStoryboard = UIStoryboard(name: "Main", bundle:NSBundle.mainBundle())

            if let myVC = aStoryboard.instantiateViewControllerWithIdentifier("ArtilceShowView") as? ArticleShowViewController  {

                myVC.urlStr = "https://www.baidu.com"

                let cellFrame = tableView.cellForRowAtIndexPath(touchedIndexPath)!.frame
                previewingContext.sourceRect = view.convertRect(cellFrame, fromView: tableView)



                return myVC  
            }  
        }

        return UIViewController()
    }

更多代码查看ArticlesTableViewController文件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值