Peek and pop in the 3D touch, like shortcut, can reduce the level of app, allowing users to see content more quickly without jumping back and forth between controller.
1, Peek and pop function description
With the IOS9 system's own SMS capabilities, PEEK and POP functions
1.1, Peek
The user holds down a cell to display the text message, after a little force, will see the message content which is pressed along with the cell to float the above display, other cell becomes unreal, at this time is in peek the shape body. As shown in the following illustration:
1.2, Pop
In Peek State, the user presses again, at this time the text that is pressed will display the window situation in the screen, the following figure:
The above two from the network interception, only used to explain the phenomenon, if there is infringement please inform, delete immediately
2, Peek and pop function realization
To implement Peek and pop preview, we have to implement the methods in the Uiviewcontrollerpreviewingdelegate protocol, which have the following methods:
Previewingcontext:viewcontrollerforlocation Peek needs to implement the Proxy method
The proxy method that Previewingcontext:commitviewcontroller pops need to implement
Now here's a real code that shows you how to implement Peek & POP functionality, create a project with two Viewcontroller
Rootviewcontroller window Rootcontroller, there is only one navigation and one TableView
Detailviewcontroller details interface, only one label, for pop popup display
Rootviewcontroller+uiviewcontrollerpreviewing Implementation Uiviewcontrollerpreviewingdelegate Protocol (the core of Peek & Pop)
The source code of each file is posted below, the specific explanation looks at the annotation
Rootviewcontroller.swift:
Import Uikit class Rootviewcontroller:uiviewcontroller, Uitableviewdatasource, uitableviewdelegate {@IBOutlet weak
var maintableview:uitableview! struct Previewdata {//preview of the data content used let title:string//title Let Preferredheight:double
Pop dialog Height} let Celldatas = [Previewdata (title: "Small", preferredheight:160.0),
Previewdata (title: "Middle", preferredheight:320.0), Previewdata (title: "Big", preferredheight:0.0)//system default to the height] Required init? (Coder Adecoder:nscoder) {Super.init (Coder:adecoder)} override init (Nibname nibnameornil:string?, bundle nibbundleornil:n Sbundle?) {Super.init (Nibname:nibnameornil, Bundle:nibbundleornil)} override func Viewdidload () {s Uper.viewdidload () Maintableview.datasource = self maintableview.delegate = self maintableview.ro Wheight = 80.0//Do any additional setupAfter loading the view. } override func Viewdidappear (animated:bool) {if self.traitCollection.forceTouchCapability = =.
Available {///If the phone supports 3D touch, the registration agent registerforpreviewingwithdelegate (self, SourceView:self.view)} } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of, any Reso
Urces can be recreated. } func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {return Celldatas.cou NT} func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath)-> UITableViewCe
ll {var Cell:uitableviewcell = Tableview.dequeuereusablecellwithidentifier ("cell") if cell = = Nil {
Cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle, Reuseidentifier: "Cell")}
Return cell! } func TableView (Tableview:uitableview, Willdisplaycell Cell:uitaBleviewcell, Forrowatindexpath indexpath:nsindexpath) {Let previewdetail = Celldatas[indexpath.row] Cell.textlabel? Text = Previewdetail.title}}
Rootviewcontroller+uiviewcontrollerpreviewing.swift:
Import uikit extension rootviewcontroller:uiviewcontrollerpreviewingdelegate {//mark:---Peek---func preview Ingcontext (previewingcontext:uiviewcontrollerpreviewing, viewcontrollerforlocation location:cgpoint)-> Uiviewcontroller? {//Get the current user-pressed cell let cellposition = Self.mainTableView.convertPoint (location, FromView:self.view)//will be used The position of the user in view is transformed into the position in the UITableView in order to judge the position of the cell placed by the users, which is very important guard let Indexpath = Maintableview.indexpathforrowatpoin T (cellposition), cell = Maintableview.cellforrowatindexpath (Indexpath) else {return nil}//new a detail object that the user Pop pops up using let Detailcontroller = Detailviewcontroller (nibname: "Detailviewcontroller", Bundle:nil) let Prev Iewdetail = Celldatas[indexpath.row] Detailcontroller.showtitle = previewdetail.title//Set pop out window The size//width is set to 0 to indicate that no size is required for vertical display, and the system defaults to control Detailcontroller.preferredcontentsize = Cgsize (width:0.0, Height:pre Viewdetail.preferreDheight///Set Peek size to cell size to ensure that, in addition to being pressed by the cell highlighting, other places blur display Previewingcontext.sourcerect = Cell.frame Return Detailcontroller}//mark:---Pop---func previewingcontext (previewingcontext: Uiviewcontrollerpreviewing, Commitviewcontroller Viewcontrollertocommit:uiviewcontroller) {ShowViewController (v Iewcontrollertocommit, Sender:self)}}
Detailviewcontroller.swift:
Import Uikit class Detailviewcontroller:uiviewcontroller {@IBOutlet weak var showlabel:uilabel!
var showtitle:string? Lazy var previewactions: [Uipreviewactionitem] = {//define the optional Action menu that appears at the bottom when the user slides up after the pop out dialog box func previewact Ionfortitle (title:string, Style:uipreviewactionstyle =. Default)-> uipreviewaction {return uipreviewaction (Title:title, Style:style) {previewaction, viewcontr Oller in guard let Detailcontroller = Viewcontroller as?
Detailviewcontroller, item = Detailcontroller.showtitle else {return} This is just a simple output of which action the user is currently clicking on, and the reader can add what he or she needs to do here. Print ("\ (previewaction.title) triggered From ' Detailviewcontroller ' to item: \ (item)}} let Action1 = Previewactionforti
Tle ("Action1") Let Action2 = Previewactionfortitle ("Action2", style:UIPreviewActionStyle.Destructive) return [Action1, Action2]} () required init? (Coder Adecoder:nscoder) {Super.init (Coder:adecoder)} override init (Nibname nibnameornil:string?, bundle nibbundleornil:n Sbundle?) {Super.init (Nibname:nibnameornil, Bundle:nibbundleornil)} override func Viewdidload () {s
Uper.viewdidload ()//Do no additional setup after loading the view. If let detail = showtitle {showlabel.text = detail}} override Func didreceivememorywarning
() {super.didreceivememorywarning ()//Dispose of any of the can is recreated.
}//Overloaded menu override Func Previewactionitems ()-> [Uipreviewactionitem] {return previewactions} @IBAction func Backbuttonclick (sender:anyobject) {Self.navigationcontroller? Popviewcontrolleranimated (True)}
At this point, Peek & Pop introduced, the hand has a kidney 6s can play this function, online there is said on the GitHub has a plug-in on the simulator to play, I have not tried, interested can try.