ios - Create a Paging UICollectionView with Swift -


i'm trying create uicollectionview paging , each item max width 250 points, i've managed create it, have 2 problems: first item start not should be, more space @ start , when try swipe, there wont let me swipe smooth.

this how looks:

video link

this code:

centercellcollectionflowlayout.swift

class centercellcollectionviewflowlayout: uicollectionviewflowlayout {      override func layoutattributesforelementsinrect(rect: cgrect) -> [anyobject]? {          var attributestoreturn:[uicollectionviewlayoutattributes] = super.layoutattributesforelementsinrect(rect) as! [uicollectionviewlayoutattributes]          var = 0 ; < attributestoreturn.count ; i++         {             var currentlayoutattributes: uicollectionviewlayoutattributes = attributestoreturn[i]             var maximumspacing: cgfloat = 50             let origin: cgfloat             if - 1 >= 0 {                 let previouslayoutattributes = attributestoreturn[i - 1]                 origin = previouslayoutattributes.frame.maxx             } else {                 origin = 0             }              if origin + maximumspacing + currentlayoutattributes.frame.size.width < self.collectionviewcontentsize().width             {                 var frame: cgrect = currentlayoutattributes.frame                 frame.origin.x = origin + maximumspacing                 currentlayoutattributes.frame = frame             }         }          return attributestoreturn      }      override func targetcontentoffsetforproposedcontentoffset(proposedcontentoffset: cgpoint, withscrollingvelocity velocity: cgpoint) -> cgpoint {          if let cv = self.collectionview {              let cvbounds = cv.bounds             let halfwidth = cvbounds.size.width * 0.5;             let proposedcontentoffsetcenterx = proposedcontentoffset.x + halfwidth;              if let attributesforvisiblecells = self.layoutattributesforelementsinrect(cvbounds) as? [uicollectionviewlayoutattributes] {                  var candidateattributes : uicollectionviewlayoutattributes?                 attributes in attributesforvisiblecells {                      // == skip comparison non-cell items (headers , footers) == //                     if attributes.representedelementcategory != uicollectionelementcategory.cell {                         continue                     }                      if let candattrs = candidateattributes {                          let = attributes.center.x - proposedcontentoffsetcenterx                         let b = candattrs.center.x - proposedcontentoffsetcenterx                          if fabsf(float(a)) < fabsf(float(b)) {                             candidateattributes = attributes;                         }                      }                     else { // == first time in loop == //                          candidateattributes = attributes;                         continue;                     }                   }                  return cgpoint(x : candidateattributes!.center.x - halfwidth, y : proposedcontentoffset.y);              }          }          // fallback         return super.targetcontentoffsetforproposedcontentoffset(proposedcontentoffset)     } } 

mainviewcontroller.swift

class mainviewcontroller: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdatasource, uicollectionviewdelegateflowlayout {      @iboutlet weak var collectionview: uicollectionview!     @iboutlet weak var collectionviewflowlayout: centercellcollectionviewflowlayout!      var collectionobjects: nsmutablearray?     private let reuseidentifier = "collectionviewcell"      override func viewdidload() {         super.viewdidload()          // additional setup after loading view.          self.collectionview.registerclass(uicollectionviewcell.self, forcellwithreuseidentifier: reuseidentifier)      }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath)     {     }      func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell     {         let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! uicollectionviewcell         cell.backgroundcolor = uicolor.greencolor()         // configure cell         return cell     }      func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int     {         return 10     }      func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, insetforsectionatindex section: int) -> uiedgeinsets     {         return uiedgeinsetsmake(0, 0, 0, 0)     }      func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize     {         return cgsizemake(250, 250)     } } 

thanks in advance

so figure out how it.

first create custom uicollectionviewflowlayout , add override method:

override func targetcontentoffsetforproposedcontentoffset(proposedcontentoffset: cgpoint, withscrollingvelocity velocity: cgpoint) -> cgpoint {      if let cv = self.collectionview {          let cvbounds = cv.bounds         let halfwidth = cvbounds.size.width * 0.5;         let proposedcontentoffsetcenterx = proposedcontentoffset.x + halfwidth;          if let attributesforvisiblecells = self.layoutattributesforelementsinrect(cvbounds) as? [uicollectionviewlayoutattributes] {              var candidateattributes : uicollectionviewlayoutattributes?             attributes in attributesforvisiblecells {                  // == skip comparison non-cell items (headers , footers) == //                 if attributes.representedelementcategory != uicollectionelementcategory.cell {                     continue                 }                  if let candattrs = candidateattributes {                      let = attributes.center.x - proposedcontentoffsetcenterx                     let b = candattrs.center.x - proposedcontentoffsetcenterx                      if fabsf(float(a)) < fabsf(float(b)) {                         candidateattributes = attributes;                     }                  }                 else { // == first time in loop == //                      candidateattributes = attributes;                     continue;                 }               }              return cgpoint(x : candidateattributes!.center.x - halfwidth, y : proposedcontentoffset.y);          }      }      // fallback     return super.targetcontentoffsetforproposedcontentoffset(proposedcontentoffset) } 

then on class implement uicollectionview that:

    let collectionviewlayout: centercellcollectionviewflowlayout = centercellcollectionviewflowlayout()     collectionviewlayout.itemsize = cgsizemake(self.itemsize, self.itemsize)     collectionviewlayout.sectioninset = uiedgeinsets(top: 0, left: 0, bottom: 0, right: 0)     collectionviewlayout.minimuminteritemspacing = 0     collectionviewlayout.minimumlinespacing = self.itemspacing     collectionviewlayout.scrolldirection = uicollectionviewscrolldirection.horizontal      var collectionview: uicollectionview = uicollectionview(frame: self.collectioncontainer.bounds, collectionviewlayout: collectionviewlayout)     collectionview.delegate = self;     collectionview.datasource = self;     collectionview.backgroundcolor = uicolor.redcolor()      collectionview.registerclass(levelscustomcell.self, forcellwithreuseidentifier: reuseidentifier)     collectionview.registernib(uinib(nibname: reuseidentifier, bundle: nil), forcellwithreuseidentifier: reuseidentifier)      self.collectioncontainer.addsubview(collectionview) 

thats it, works charm.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -