Home Screen Quick Actions
- 静态 Quick Actions
- 动态 Quick Actions
静态 Quick Actions
编辑 Info.plist
文件,如下图:
源码如下:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeCompose</string>
<key>UIApplicationShortcutItemTitle</key>
<string>标签1</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.item1</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemSubtitle</key>
<string>副标题</string>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeCloud</string>
<key>UIApplicationShortcutItemTitle</key>
<string>标签2</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.item2</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
</dict>
</array>
动态 Quick Actions
AppDelegate
文件中添加如下代码:
private func handleDynamicQuickActions() {
if 0 == UIApplication.sharedApplication().shortcutItems?.count {
let newItem = UIApplicationShortcutItem(type: "com.mycompany.myapp.item3", localizedTitle: "dynamic标签3")
UIApplication.sharedApplication().shortcutItems?.append(newItem)
}
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
handleDynamicQuickActions()
return true
}
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
let type = shortcutItem.type
if "com.mycompany.myapp.item1" == type {
print("item1 selected")
} else if "com.mycompany.myapp.item2" == type {
print("item2 selected")
} else if "com.mycompany.myapp.item3" == type {
print("item3 selected")
}
}
运行效果:
效果图Peek and Pop
当前 ViewController
class ViewController: UIViewController, UIViewControllerPreviewingDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Add test button
let btn = UIButton(type: .System)
btn.setTitle("Peek & Pop Test: click me", forState: .Normal)
btn.sizeToFit()
btn.backgroundColor = UIColor.yellowColor()
btn.center = self.view.center
self.view.addSubview(btn)
// register preview
if .Available == traitCollection.forceTouchCapability {
registerForPreviewingWithDelegate(self, sourceView: btn)
}
}
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
return DetailViewController()
}
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
showViewController(viewControllerToCommit, sender: self)
}
}
DetailViewController
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
}
override func previewActionItems() -> [UIPreviewActionItem] {
let action1 = UIPreviewAction(title: "Action 1", style: .Default) { (action, viewController) -> Void in
print("Action 1 selected")
}
let action2 = UIPreviewAction(title: "Action 2", style: .Destructive) { (action, viewController) -> Void in
print("Action 2 selected")
}
return [action1, action2]
}
}
运行效果图:
效果图1 效果图2 效果图3
网友评论