首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios -动态编辑3d触摸快捷键列表

ios -动态编辑3d触摸快捷键列表
EN

Stack Overflow用户
提问于 2015-12-05 17:51:17
回答 4查看 4.9K关注 0票数 8

我想添加一个“继续”的捷径给我的游戏。但当用户将完成我的游戏完全,我希望这要么是删除或替换为另一个快捷方式。这个是可能的吗?我知道3d触摸是由ios系统处理的,但可能还有一些选择。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-12-05 19:35:36

有两种方法来创建快捷方式-动态和静态。

  • 静态被添加到plist中,并且不会改变。
  • 动态可以在代码中添加和删除。

听起来你想要一个动态的快捷方式,下面大概是这样做的:

增加:

代码语言:javascript
复制
if #available(iOS 9.0, *) {
    if (UIApplication.sharedApplication().shortcutItems?.filter({ $0.type == "com.app.myshortcut" }).first == nil) {
        UIApplication.sharedApplication().shortcutItems?.append(UIMutableApplicationShortcutItem(type: "com.app.myshortcut", localizedTitle: "Shortcut Title"))
    }
}

移除:

代码语言:javascript
复制
if #available(iOS 9.0, *) {
    if let shortcutItem = UIApplication.sharedApplication().shortcutItems?.filter({ $0.type == "com.app.myshortcut" }).first {
        let index = UIApplication.sharedApplication().shortcutItems?.indexOf(shortcutItem)

        UIApplication.sharedApplication().shortcutItems?.removeAtIndex(index!)
    }
}

然后,可以通过在app委托方法中检查快捷方式来处理快捷方式:

代码语言:javascript
复制
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    if shortcutItem.type == "com.app.myshortcut" {
        // Do something
    }
}

不要忘记检查iOS9和3d触摸兼容性。

您可以在这里找到Apple developer 3d触摸页面:

https://developer.apple.com/ios/3d-touch/

特别是动态快捷键:

3.

票数 11
EN

Stack Overflow用户

发布于 2016-07-20 21:33:19

这里有一个方便的类,可以在你的应用程序图标上触发3D触点。当然,您可以触发任何操作,但这可能是最常见的。然后,当应用程序出现或转到后台时,它会同步自己。只有在用户生成一个(VisualizerProject.currentProject.images.count >0之后,我才使用它触发一个"My“部分)。

代码语言:javascript
复制
class AppShortcut : UIMutableApplicationShortcutItem {
    var segue:String

    init(type:String, title:String, icon:String, segue:String) {
        self.segue = segue
        let translatedTitle = NSLocalizedString(title, comment:title)
        let iconImage = UIApplicationShortcutIcon(templateImageName: icon)
        super.init(type: type, localizedTitle:translatedTitle, localizedSubtitle:nil, icon:iconImage, userInfo:nil)
    }
}

class AppShortcuts {

    static var shortcuts:[AppShortcut] = []

    class func sync() {

        var newShortcuts:[AppShortcut] = []

        //reverse order for display
        newShortcuts.append(AppShortcut(type: "find-color", title: "Find Color", icon:"ic_settings_black_24px", segue: "showColorFinder"))

        newShortcuts.append(AppShortcut(type: "samples", title: "Sample Rooms", icon:"ic_photo_black_24px", segue: "showSamples"))

        //conditionally add an item like this:
        if (VisualizerProject.currentProject.images.count > 0) {
            newShortcuts.append(AppShortcut(type: "projects", title: "My Projects", icon:"ic_settings_black_24px", segue: "showProjects"))
        }

        newShortcuts.append(AppShortcut(type: "visualizer", title: "Paint Visualizer", icon:"ic_photo_camera_black_24px", segue: "showPainter"))

        UIApplication.sharedApplication().shortcutItems = newShortcuts
        shortcuts = newShortcuts
    }

    class func performShortcut(window:UIWindow, shortcut:UIApplicationShortcutItem) {

        sync()

        if let shortcutItem = shortcuts.filter({ $0.type == shortcut.type}).first {

            if let rootNavigationViewController = window.rootViewController as? UINavigationController,
                let landingViewController = rootNavigationViewController.viewControllers.first {
                //Pop to root view controller so that approperiete segue can be performed
                rootNavigationViewController.popToRootViewControllerAnimated(false)

                landingViewController.performSegueWithIdentifier(shortcutItem.segue, sender: self)
            }
        }
    }
}

然后在应用程序委托中,添加同步并执行快捷调用。

代码语言:javascript
复制
func applicationDidEnterBackground(application: UIApplication) {
    AppShortcuts.sync()
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    AppShortcuts.sync()
}

@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

    if let window = self.window {
         AppShortcuts.performShortcut(window, shortcut: shortcutItem)
    }
}
票数 4
EN

Stack Overflow用户

发布于 2015-12-05 20:27:28

我想你说的是用户可以强行调用的所谓的快速动作--在主屏幕上触摸应用程序的图标。您可以从代码中动态地创建和更新它们。了解所有可能性的最好方法是看看苹果的样本代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34108818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档