Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了3D Touch swift 版本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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 // // Cr

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

大佬总结

以上是大佬教程为你收集整理的3D Touch swift 版本全部内容,希望文章能够帮你解决3D Touch swift 版本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: