3D Touch swift 版本

3 篇文章 0 订阅

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, animated: true, 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: "收藏", style: UIPreviewActionStyle.Default) { (<#UIPreviewAction#>, <#UIViewController#>) -> Void in
            print("点击了收藏")
        }
        return [shareItem,savingItem]
    }
    
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值