Protein
框架的主要目的是简化在 iOS 中对 3D touch 的操作。与 iOS 8 兼容。
Source
文件夹的内容复制到您的项目中。或
Protein
cocoapod为了使用 Protein
管理快捷项目,您需要将项目 Info.plist
文件中的项目列表添加到。以下是一个示例
在此示例中,我们将使用两个快捷项目。一个是启动蓝色屏幕的应用程序,另一个是启动橙色屏幕的应用程序。
当您在 Info.plist
文件中描述了结构后,您需要将这些条目注册到 Protein
引擎中。为此,从 PTAppDelegate
类中派生您的 AppDelegate
。
@UIApplicationMain
class AppDelegate: PTAppDelegate {
var window: UIWindow?
var mainViewController: UIViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Register shortcut items
registerShortcutItemType("blue-screen") { (shortcutItem) -> Void in
self.mainViewController?.view.backgroundColor = .cyanColor()
}
registerShortcutItemType("orange-screen") { (shortcutItem) -> Void in
self.mainViewController?.view.backgroundColor = .orangeColor()
}
// Create window
let frameForWindow = UIScreen.mainScreen().bounds
window = UIWindow(frame: frameForWindow)
window!.backgroundColor = .whiteColor()
window!.makeKeyAndVisible()
// Switch to view controller
mainViewController = UIViewController()
let navigationController = UINavigationController(rootViewController: mainViewController!)
navigationController.navigationBarHidden = true
window!.rootViewController = navigationController
// Return result
return true
}
}
PTAppDelegate
包含一个名为 registerShortcutItemType(_:, _:)
的方法。此方法注册特定类型快捷项目的事件处理程序块。当用户单击快捷项目时,将调用此块。
注意:如果您的项目支持 iOS 8,则需要将 registerShortcutItemType(_:, _)
的调用包裹在特殊条件中
if #available(iOS 9, *) {
registerShortcutItemType("blue-screen") { (shortcutItem) -> Void in
self.mainViewController?.view.backgroundColor = .cyanColor()
}
registerShortcutItemType("orange-screen") { (shortcutItem) -> Void in
self.mainViewController?.view.backgroundColor = .orangeColor()
}
}
此外,为了完整性,PTAppDelegate
为开发者提供了 unregisterShortcutItemType(_:)
方法。但通常没有理由调用此方法。
Protein
在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE
文件。