ios - UICollectionView scrolling/swipe to next item swift -
i have uicollectionview in view controller. when user clicks 1 of images, goes separate page open image. instead of going gallery each time change pictures, swipe left or right next or previous picture.
i have seen answers "pagingenabled = true" put exactly?
i new swift.
below code. in advance.
viewcontroller.swift
import uikit class viewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate { @iboutlet weak var collectionview: uicollectionview! let appleproducts = ["iphone", "apple watch", "mac", "ipad"] let imagearray = [uiimage(named: "pug"), uiimage(named: "pug2"), uiimage(named: "pug3"), uiimage(named: "pug4")] override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return self.appleproducts.count } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier("cell", forindexpath: indexpath) as! collectionviewcell cell.imageview?.image = self.imagearray[indexpath.row] cell.titlelabel?.text = self.appleproducts[indexpath.row] return cell } func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { self.performseguewithidentifier("showimage", sender: self) } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "showimage" { let indexpaths = self.collectionview!.indexpathsforselecteditems()! let indexpath = indexpaths[0] nsindexpath let vc = segue.destinationviewcontroller as! newviewcontroller vc.image = self.imagearray[indexpath.row]! vc.title = self.appleproducts[indexpath.row] } } }
collectionviewcell.swift
import uikit class collectionviewcell: uicollectionviewcell { @iboutlet weak var imageview: uiimageview! @iboutlet weak var titlelabel: uilabel! }
newviewcontroller.swift
import uikit class newviewcontroller: uiviewcontroller { @iboutlet weak var imageview: uiimageview! var image = uiimage() override func viewdidload() { super.viewdidload() self.imageview.image = self.image } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ }
in view controller presented after tap, should make horizontally scrolling uicollectionview
. on collection view, set pagingenabled
property true.
the next thing reformat prepareforsegue
method in parent view controller pass entire array of images presented view controller selected index path.
then, in presented view controller, should have collection view's data source configured show images make sure start @ initial, selected index path passed parent view controller.
Comments
Post a Comment