iOS - 动态编辑3D Touch快捷方式列表

8

我希望在我的游戏中添加一个“继续”快捷方式。但是当用户完全完成我的游戏时,我希望它要么被删除,要么被替换为另一个快捷方式。这是否可能?我知道3D Touch由iOS系统处理,但也许仍有一些选项。


快捷键“继续”在哪里?另外,您所说的快捷键是指按钮吗? - FredLoh
我的游戏是基于关卡的。因此,这个快捷方式是UIApplicationShortCut(出现在主iOS菜单中),旨在启动最后一个可用的关卡。 - Andrew Volodin
4个回答

11

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

  • 静态的快捷方式被添加到 plist 中并且永远不会更改。
  • 动态的快捷方式可以在代码中添加和删除。

看起来您想要一个动态的快捷方式,以下大致是如何实现:

要添加:

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

移除:

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

您可以通过检查应用程序委托方法中的快捷方式来处理它:

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

别忘了检查 iOS9 和 3D Touch 的兼容性。

你可以在这里找到苹果开发者有关 3D Touch 的页面:

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

特别是这里的动态快捷方式:

https://developer.apple.com/library/ios/samplecode/ApplicationShortcuts/Listings/ApplicationShortcuts_AppDelegate_swift.html#//apple_ref/doc/uid/TP40016545-ApplicationShortcuts_AppDelegate_swift-DontLinkElementID_3


看起来它似乎符合我的需求,唯一令人遗憾的是,在从AppStore安装应用程序后,您无法设置动态快捷方式。所以我想,只有在一段时间过去后,我的快捷方式才会出现,这也没关系。谢谢你的回答,稍后我会试一试,如果有效,我会将其标记为正确! - Andrew Volodin
我同意,我们有一个登录快捷方式的很好的例子。展示从应用下载时开始的登录,并在用户登录后删除这个功能会很不错。如果有解决方案,我们很感兴趣,但目前还没有找到。无论如何,很高兴它对你有所帮助,谢谢反馈。 - Jamie Wheeldon
有人尝试过这段代码吗?如果shortcut items不是可变数组,你该如何直接从中删除?你应该只是创建一个空数组并将其设置为替代吗? - skinsfan00atg
@skinsfan00atg 我认为它是可变的。我可以从我的 didFinishLaunchingWithOptions 中执行此操作:application.shortcutItems?.append(myShortcut)。当你使用 it let ... 时,当然,let 将不会被改变。那么如果用 if var shortcuts = UIApplication... 呢? - bauerMusic

4

这是一个便捷的类,可以通过3D Touch触发您的应用程序图标上的转场。当然你也可以触发任何其他操作,但这可能是最常见的操作。它会在应用程序启动或进入后台时自动同步。我正在使用它来触发“我的项目”部分,只有在用户生成了一个项目(VisualizerProject.currentProject.images.count > 0)之后才会触发。

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

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

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

0
 [UIApplication sharedApplication].shortcutItems = @[];

对我有用。另一个建议从数组中删除某些内容的答案不起作用,因为shortcutItems是不可变的。


0

我猜你在谈论所谓的“快速操作”,用户可以通过强制按压您的应用程序图标来调用它们。您可以直接从代码中动态创建和更新它们。了解所有可能性的最佳方法是查看苹果的示例代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接