ios-动态编辑3d触摸快捷方式列表

ios-动态编辑3d触摸快捷方式列表,ios,3dtouch,Ios,3dtouch,我想为我的游戏添加一个“继续”快捷方式。但当用户将完全完成我的游戏,我想这是要么删除或替换为另一个快捷方式。这可能吗?我知道3d touch由ios系统处理,但可能还有一些选项创建快捷方式有两种方法-动态和静态 静态数据将添加到plist中,并且永远不会更改 可以在代码中添加和删除动态 听起来你想要一个动态的快捷方式,下面是你应该怎么做的: 加上: if #available(iOS 9.0, *) { if (UIApplication.sharedApplication().sh

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

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

  • 静态数据将添加到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!)
    }
}
然后,您可以通过在app delegate方法中检查快捷方式来处理该快捷方式:

@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 touch页面:

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


我想你说的是所谓的快速行动,用户可以通过强制触摸主屏幕上的应用程序图标来调用。您可以直接从代码中动态创建和更新它们。了解所有可能性的最好方法是看看苹果的产品


为我工作。另一个答案是,从数组中删除某些东西不能作为shortcutitem使用,这是不可变的

这里有一个方便的类,可以触发应用程序图标的3D触控。当然,你可以触发任何动作,但这可能是最常见的。然后,当应用程序出现或进入后台时,它会自动同步。只有在用户生成一个“我的项目”部分(VisualizerProject.currentProject.images.count>0)后,我才使用它触发一个“我的项目”部分

然后在应用程序代理中,添加同步并执行快捷方式调用

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

“继续”快捷方式放在哪里?我猜你说的快捷方式是指按钮?我的游戏是基于等级的。因此,这个快捷方式是UIApplicationShortCut(出现在ios主菜单中),旨在启动最后一个可用级别。看起来它会满足我的需要,唯一令人遗憾的是,从AppStore安装应用程序后,您无法设置动态快捷方式。所以我想我的快捷方式只有在一段时间后才会出现是可以的。谢谢你的回答,我会稍后再试一次,如果成功,我会把它标记为正确!我同意,我们有一个登录快捷方式的好例子。最好从下载应用程序的位置显示登录,然后在用户登录时将其删除。我很想知道是否有解决方案,但我们还没有找到。不管怎样,很高兴它有帮助,谢谢你的反馈。有人试过这个代码吗?如果快捷方式项不是可变数组,如何直接从中删除?您是否应该创建一个空数组并设置它?@skinsfan00atg我认为它是可变的。我可以通过我的
didfishlaunchingwithoptions
application.shortcutItems?.append(myShortcut)
完成此操作。当你这样做的时候,
让…
,当然,
不会。如果var shortcuts=UIApplication…如何。
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)
    }
}