IOS implementation of 3D touch in TableView simple application (Swift version)

Source: Internet
Author: User

Previously recorded in the OC version to achieve 3D touch function of a small demonstration, recently nothing full swift, also get a swift version of 3D Touoch ...

On the code.


One, 3D touch Press the main screen icon there are two ways to add shortcut tags:1. Static add, in the project info.plistFile, this way the added label can be displayed after installation

2. Dynamically add, add fast portals dynamically via code. Tags added in this way must be run one time before the app appears.

func Application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)        Bool {//Override point for customization after application launch. Self.window?. frame = Uiscreen.mainscreen (). Bounds Self.window?                Makekeyandvisible () uiapplication.sharedapplication (). Statusbarstyle = Uistatusbarstyle.lightcontent Self.window = UIWindow (Frame:UIScreen.mainScreen (). Bounds) Self.window?. Makekeywindow () Self.configshortcutitems () Self.window?. Rootviewcontroller = Mcuitabbarcontroller () return true} 
Func Configshortcutitems ()    {        //Dynamic Add mode let        Icon1 = Uiapplicationshortcuticon (type:. Play) Let        item1 = Uimutableapplicationshortcutitem (type: "777", Localizedtitle: "title", Localizedsubtitle: "subtitle", Icon:icon1, Userinfo:nil) let        icon2 = Uiapplicationshortcuticon (type:. ADD) Let        item2 = Uimutableapplicationshortcutitem (Type: "888", Localizedtitle: "title", Localizedsubtitle: "subtitle", Icon : Icon2, Userinfo:nil) let        icon3 = Uiapplicationshortcuticon (templateimagename: "Shareicon") let        item3 = Uimutableapplicationshortcutitem (Type: "999", Localizedtitle: "Custom Picture", Localizedsubtitle:nil, Icon:icon3, UserInfo: Nil)        uiapplication.sharedapplication (). Shortcutitems = [Item1,item2,item3]                    }


It is important to note that the main screen calls out the shortcut tag, static and dynamic add a total of only four.

3D Touch in Appdelegate.swift Proxy method:

pragma mark-3d Touch    func application (application:uiapplication, Performactionforshortcutitem Shortcutitem: Uiapplicationshortcutitem, Completionhandler: (Bool), Void) {        switch shortcutitem.type {case        "666":            Print ("static add") case "        777":            print ("Dynamic code Add one") Case        "888":            print ("Dynamic code Add two") Case        "999":            Print ("Custom Picture")        Default:            print ("")        }    }


At this point , by re-pressing the main screen trigger 3D touch to call out the Shortcut tab and click the separate response operation has been integrated!

Second, 3D Touch of Peek and pop function on the tableview simple implementation

Words don't say much, directly on the code

1.peek Preview

Dddtouchvc.swift

3D Touch Agent controller needs to implement   uiviewcontrollerpreviewingdelegate   protocol

Import Uikitclass Dddtouchvc:mcbaseviewcontroller,uitableviewdatasource,uitableviewdelegate,    uiviewcontrollerpreviewingdelegate {static Let Cellidentifer = "MyCell" var tableview:uitableview? var Iscan3dtouch = true var itemarray:array = [Dictionary<string,string>] () override Func Viewdidload ()        {super.viewdidload ()//Do any additional setup after loading the view. Self.setcustomtitle ("3D Touch") Self.initdata () Self.initsubview () if traitcollection.forcetouchcapab            Ility = = uiforcetouchcapability.available {Self.iscan3dtouch = true} else { Self.iscan3dtouch = False}} func InitData () {Let Dic1 = ["M": "Http://a.hiphotos.baidu.com/im Age/h%3d360/sign=1698b41ebb389b5027ffe654b535e5f1/a686c9177f3e6709e974ebff39c79f3df8dc55bd.jpg "] Let Dic2 = [" MM ": "Http://b.hiphotos.baidu.com/image/h%3D360/sign=ff34dff8bd389b5027ffe654b537e5f1/a686c9177f3e670900d880193fc79f3df9dc5578.jpg "] Let dic3 = [" MMM ":" http://a.hiphotos.baidu.com/image/h%3D360/sign=f527ab2f4b5409 23b5696578a259d1dc/dcc451da81cb39dbce6b325ad2160924ab183016.jpg "] Let dic4 = [" MMMM ":" http://d.hiphotos.baidu.com/ Image/h%3d360/sign=81d5d22779f40ad10ae4c1e5672d1151/d439b6003af33a8762fee505c45c10385343b51a.jpg "] Let dic5 = [" mm MMM ":" http://d.hiphotos.baidu.com/image/h%3D200/sign=8d3a52a04ded2e73e3e9812cb701a16d/ F7246b600c33874450b89258560fd9f9d72aa091.jpg "] Let dic6 = [" Mmmmmm ":" Http://h.hiphotos.baidu.com/image/h%3D360/sig N=e53dee25d5ca7bcb627bc1298e086b3f/a2cc7cd98d1001e9ae32c3e9ba0e7bec55e797ce.jpg "] Let Dic7 = [" Mmmmm ":" Http://g.hi photos.baidu.com/image/h%3d360/sign=8cb0e6191a178a82d13c79a6c602737f/ 6c224f4a20a446230761b9b79c22720e0df3d7bf.jpg "] Let Dic8 = [" MMMM ":" http://a.hiphotos.baidu.com/image/h%3D360/sign= 80285822f9f2b211fb2e8348fa806511/bd315c6034a85edf1da1c0724b540923dd5475b5.jpg "] Let dic9 = [" MMM ":" Http://e.hiphotos.baidu.com/image/h%3d360/sign=708629f379cb0a469a228d3f5b62f63e/ 7dd98d1001e939015d4a463779ec54e736d1966b.jpg "] Let DIC10 = [" MM ":" Http://h.hiphotos.baidu.com/image/h%3D360/sign=9 5102c30bc096b639e1958563c338733/3801213fb80e7beccbb1e8fe2d2eb9389b506b42.jpg "] Let dic11 = [" M ":" Http://a.hiphotos        . Baidu.com/image/h%3d360/sign=cbd227bf8e5494ee9822091f1df5e0e1/c2cec3fdfc039245ee84470f8594a4c27d1e25f8.jpg "] Let DIC12 = ["MM": "http://e.hiphotos.baidu.com/image/h%3D360/sign=87824d652adda3cc14e4be2631e83905/ B03533fa828ba61e64671cd54534970a314e59bb.jpg "] Itemarray.append (DIC1) itemarray.append (DIC2) ItemArra Y.append (DIC3) itemarray.append (DIC4) itemarray.append (DIC5) itemarray.append (DIC6) ItemArray. Append (Dic7) itemarray.append (DIC8) itemarray.append (DIC9) itemarray.append (DIC10) itemarray.a Ppend (DIC11) itemarray.append (DIC12)} func Initsubview () {Self.tableview = UITabLeview (frame:cgrectmake (0, 0, Mcconstant.screenwidth, mcconstant.screenheight), Style:UITableViewStyle.Plain) sel F.tableview?. BackgroundColor = Uicolor.clearcolor () Self.tableview?. DataSource = Self Self.tableview?.    delegate = self Self.view.addSubview (self.tableview!) } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any RESOURC    Es that can is recreated. } func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {return itemarray.co  UNT} func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell            {var cell = Tableview.dequeuereusablecellwithidentifier (moduleonevc.cellidentifer) if cell = = Nil {  Cell = UITableViewCell (Style:UITableViewCellStyle.Default, ReuseIdentifier:ModuleOneVC.cellIdentifer) if           Self.iscan3dtouch {//register 3D Touch     Registerforpreviewingwithdelegate (Self, Sourceview: (cell?.            Contentview)!) }} Let dic = Itemarray[indexpath.row] cell?. Textlabel?. Text = Array (Dic.keys) [0] cell?. Textlabel?. TextColor = Uicolor.blackcolor () cell?.    BackgroundColor = Uicolor.clearcolor () return cell! } func TableView (Tableview:uitableview, Heightforrowatindexpath indexpath:nsindexpath), cgfloat {ret Urn # func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {Tablevi Ew.deselectrowatindexpath (Indexpath, animated:true) let dic = Itemarray[indexpath.row] Self.navigationcontr Oller?. Pushviewcontroller (DDDTOUCHPEEKVC (Imgurl:array (Dic.values) [0]), animated:true)}//mark-uiviewcontrollerp Reviewingdelegate 3D Touch Proxy method func Previewingcontext (previewingcontext:uiviewcontrollerpreviewing, Viewcontrollerforlocation location:cgpoint), Uiviewcontroller?      {  Let Indexpath = Self.tableview?. Indexpathforcell (PreviewingContext.sourceView.superview as! UITableViewCell) Let dic = itemarray[indexpath!. Row] Let PEEKVC = DDDTOUCHPEEKVC (Imgurl:array (Dic.values) [0]) peekvc.preferredcontentsize = Cgsizemake (0, 4 (XX) return PEEKVC} func previewingcontext (previewingcontext:uiviewcontrollerpreviewing, Commitviewcontr Oller viewcontrollertocommit:uiviewcontroller) {Self.navigationcontroller?. Pushviewcontroller (Viewcontrollertocommit, Animated:true)}}
* * * above demonstration with the photo source are from Baidu random search.

2.pop Interface Controller

Dddtouchpeekvc.swift

Import Uikitclass Dddtouchpeekvc:mcbaseviewcontroller {var etag:string?    var potoview:uiimageview?    var imageurl:string? Init (imgurl:string) {super.init (Nibname:nil, bundle:nil) Self.imageurl = Imgurl} required init? (Coder Adecoder:nscoder) {FatalError ("init (coder:) have not been implemented")} override func Viewdidload () {Super.viewdidloa        D ()//additional setup after loading the view. Self.setcustomtitle ("3D touchpeekvc") Self.initsubview () Self.initdata ()} override Func Didreceivemem    Orywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.        } func InitData () {Let URL = nsurl.init (string:imageurl!)        Let request = Nsurlrequest (url:url!) Let task = Nsurlsession.sharedsession (). Datataskwithrequest (Request) {(DATAA, response, error) in Dispatch_asy         NC (Dispatch_get_main_queue (), {       Self.potoview?.            Image = UIImage (data:dataa!) })} task.resume ()} func Initsubview () {Let Titleview = UILabel (frame:cgrectmake (0, 0 , Mcconstant.screenwidth, +)) Titleview.backgroundcolor = Uicolor (red:52/255, green:222/255, blue:136/255, Alph a:1). Colorwithalphacomponent (0.8) titleview.textalignment = Nstextalignment.center Titleview.text = "PeekVi EW "Self.view.addSubview (titleview) Self.potoview = Uiimageview (Frame:cgrect (x:0, y:64, WIDTH:MC Constant.screenwidth, height:mcconstant.screenheight-60)) Self.potoview?    Contentmode = Uiviewcontentmode.scaletofill Self.view.addSubview (self.potoview!) }//pragma mark-bottom Preview interface option override func Previewactionitems (), [Uipreviewactionitem] {//Let act Ion1 = uipreviewaction (title: "Likes", Style:.) Default) {(action, Previewviewcontroller) in//} Let Action2 = UIPReviewaction (title: "Comment", Style:. Default) {(action, Previewviewcontroller) in//} Let Action3 = Uipreviewaction (title: "Hehe", sty Le:. Default) {(action, Previewviewcontroller) in//} Let Action4 = Uipreviewaction (title: "Hehe", sty Le:. Default) {(action, Previewviewcontroller) in//} let Actionitems = [action1,action2,action3,act ION4] return Actionitems}}

at this point, the 3D touch function in the TableView list of simple functions are completed, this is Peek, pop shows the loading of a Baidu connection picture. In haste, written in no detail, if there is a mistake, understanding ...




On




IOS implementation of 3D touch in TableView simple application (Swift version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.