ios - Implement 3D touch on multiple collection views or table views in the same view controller -


i have 3d touch on both of collection views of app(only "peek" functionality). both contained in same view controller. no matter syntax try shows image , text cell in first collectionview, if choose cell in second collection view. how separate these 1 happens collectionview cell have selected?

here how implementing 3d touch functionality:

i put in vc's viewdidappear because wasn't working in viewdidload reason:

  override func viewdidappear(animated: bool) {    if( traitcollection.forcetouchcapability == .available){      registerforpreviewingwithdelegate(self, sourceview: collectionview1)     registerforpreviewingwithdelegate(self, sourceview: collectionview2)  } 

here have in vc houses 2 collectionviews:

func previewingcontext(previewingcontext: uiviewcontrollerpreviewing, viewcontrollerforlocation location: cgpoint) -> uiviewcontroller? {   guard let previewvc = storyboard?.instantiateviewcontrollerwithidentifier("previewvc") as? previewviewcontroller     else{ return nil }  guard let indexpath = collectionview1?.indexpathforitematpoint(location) else { return nil } guard let indexpath2 = collectionview2.indexpathforitematpoint(location) else { return nil } guard let cell1 = collectionview1?.cellforitematindexpath(indexpath) else { return nil } guard let cell2 = collectionview2.cellforitematindexpath(indexpath) else { return nil}      previewvc.selecteditem = string(gifs.row1[indexpath.row])     previewvc.selecteditem2 = string(gifs.row2[indexpath.rown])      previewvc.preferredcontentsize = cgsize(width: 245, height: 200)     previewingcontext.sourcerect = cell1.frame  return previewvc } 

and here have in previewvc:

class previewviewcontroller: uiviewcontroller {    @iboutlet weak var selectedgifimageview: uiimageview!   @iboutlet weak var textlabel: uilabel!    var selecteditem: string?  var selecteditem2: string?  var delegate: movescrollviewcontroller?  override func viewwillappear(animated: bool) {   textlabel.text = gifs.gifdictionary[selecteditem]  selectedgifimageview.setgifimage(uiimage(name: selecteditem))   textlabel.text = gifs.gifdictionary[selecteditem2]  selectedgifimageview.setgifimage(uiimage(name: selecteditem2))  } 

gif of what's happening

this did in project.

i have scrollview on top of viewcontroller , tableview on bottom. , 2 of them adopt peek , pop. following did.

first, need add uiviewcontrollerpreviewingdelegate viewcontroller implement peek , pop function.

second, register container view (usually self.view) calling

if self.traitcollection.forcetouchcapability == .available {     registerforpreviewingwithdelegate(self, sourceview: view) } 

third, convert cgpoint of view coordinate system view have peek , pop feature in previewingcontext delegate

func previewingcontext(previewingcontext: uiviewcontrollerpreviewing, viewcontrollerforlocation location: cgpoint) -> uiviewcontroller? 

for example:

a. convert cgpoint on self.view point inside tableview

let tableviewpoint = tableview.convertpoint(location, fromview: view) 

b. after have point, can confirm if tableviewpoint inside tableview, tableviewcell tableviewpoint , it.

if self.tableview.pointinside(tableviewpoint, withevent: nil) {     guard let indexpath =  self.tableview.indexpathforrowatpoint(tableviewpoint) else {             return nil         }      guard let tableviewcell = self.tableview.cellforrowatindexpath(indexpath) else {             return nil         }     // tableview cell here...     let yourviewcontroller = uistoryboard(name: "main", bundle: nil).instantiateviewcontrollerwithidentifier("yourviewcontroller") as! yourviewcontroller      // set contentsize peek     yourviewcontroller.preferredcontentsize = cgsize(width: 0.0, height: 600)      // set focusing rect tableviewcell won't blurred out.     let viewpoint = self.view.convertpoint(tablecell.frame.origin, fromview: self.tableview)     previewingcontext.sourcerect = cgrect(origin: viewpoint, size: tablecell.frame.size)     return yourviewcontroller } 

and same scroll view, able have peek , pop both scrollview , tableview in same viewcontroller.

last implement second delegate pop

func previewingcontext(previewingcontext: uiviewcontrollerpreviewing, commitviewcontroller viewcontrollertocommit: uiviewcontroller) {     showviewcontroller(viewcontrollertocommit, sender: self) } 

it works me perfectly. out well.


Comments