OSX 10.10.5 (14F27)
XCODE Version 7.1.1 (7B1005)
swift -version --->Apple Swift version 2.1 (swiftlang-700.1.101.6 clang-700.1.76)
AppDelegate.swift:
// // AppDelegate.swift // 3DTouchDemo // // Created by Johnson on 15/11/13. // Copyright © 2015年 Johnson. All rights reserved. // import UIKit @UIApplicationMain class AppDelegate: UIResponder,UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //给App图标添加3D Touch菜单 重按 //拍照 //菜单图标 let cameraIcon = UIApplicationShortcutIcon(type: .Add) let cameraItem = UIMutableApplicationShortcutItem(type: "camera",localizedTitle: "拍照") cameraItem.icon = cameraIcon //给App图标添加3D Touch菜单 //拍照 let photoLibraryIcon = UIApplicationShortcutIcon(type: .Search) let photoLibrayItem = UIMutableApplicationShortcutItem(type: "photos",localizedTitle: "相册") photoLibrayItem.icon = photoLibraryIcon application.shortcutItems = [cameraItem,photoLibrayItem] return true } func application(application: UIApplication,performActionForShortcutItem shortcutItem: UIApplicationShortcutItem,completionHandler: (Bool) -> Void) { if shortcutItem.type == "camera" { let picker = UIImagePickerController() picker.allowsEditing = true picker.sourceType = UIImagePickerControllerSourceType.Camera //进入照相界面 self.window?.rootViewController?.presentViewController(picker,animated: true,completion: nil) } else if shortcutItem.type == "photos" { let libraryPicker = UIImagePickerController() libraryPicker.allowsEditing = true libraryPicker.sourceType = UIImagePickerControllerSourceType.Camera //进入图片库 self.window?.rootViewController?.presentViewController(libraryPicker,completion: nil) } } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks,disable timers,and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources,save user data,invalidate timers,and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution,this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } 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. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }
在两个application级别的监听器函数中完成:1.对图标菜单的设置 2.当点击图标后的事件分配和处理
// // ViewController.swift // 3DTouchDemo // // Created by Johnson on 15/11/13. // Copyright © 2015年 Johnson. All rights reserved. // import UIKit class ThreeDTouchViewController : UIViewController,UIViewControllerPreviewingDelegate { override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func previewingContext(previewingContext: UIViewControllerPreviewing,viewControllerForLocation location: CGPoint) -> UIViewController? { //ASPreviewViewController return nil } func previewingContext(previewingContext: UIViewControllerPreviewing,commitViewController viewControllerToCommit: UIViewController) { } //预览时,添加底部菜单 override func previewActionItems() -> [UIPreviewActionItem] { let shareItem = UIPreviewAction(title: "分享",style: UIPreviewActionStyle.Default) { (<#UIPreviewAction#>,<#UIViewController#>) -> Void in print("点击了分享") } let savingItem = UIPreviewAction(title: "收藏",<#UIViewController#>) -> Void in print("点击了收藏") } return [shareItem,savingItem] } }原文链接:https://www.f2er.com/swift/325263.html