view - Make a CollectionViewCell bounce on highlight in Swift -
for past couple of hours have tried accomplish following:
in uicollectionview when highlight (touch down) cell want bounce inward few pixels (to smaller size) , on release bounce original size.
here have tried far:
override func collectionview(collectionview: uicollectionview, didhighlightitematindexpath indexpath: nsindexpath) { collectionview.collectionviewlayout.invalidatelayout() let cell = collectionview.cellforitematindexpath(indexpath) uiview.transitionwithview(cell!, duration: 1, options: uiviewanimationoptions.curveeasein, animations: { let center = cell?.center let frame2 = cgrect(x: 0, y: 0, width: 50, height: 50) cell?.frame = frame2 cell?.center = center! }, completion: nil) }
(i have found of code example question: uicollectionview: animate cell size change on selection applied on cell selection. not highlight. )
so in didhighlightitematindexpath set highlighted cell new size. size set correct following problems arise: 1: not animate, 2: moves position (0,0) , animate center position. should not move position @ - size.
i have seen effect in shazam app reference. can seen when shazam song (or view shazamed song) on buttons below song name.
here's general gist, although doesn't handle edge cases.
class mycollectionviewcell: uicollectionviewcell { override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { uiview.animatewithduration(0.5) { () -> void in let shrinktransform = cgaffinetransformscale(cgaffinetransformidentity, 0.5, 0.5) self.transform = shrinktransform } } override func touchesended(touches: set<uitouch>, withevent event: uievent?) { super.touchesended(touches, withevent: event) uiview.animatewithduration(0.5) { () -> void in self.transform = cgaffinetransformidentity } } } class viewcontroller: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdatasource { let l = uicollectionviewflowlayout() var c: uicollectionview! = nil override func viewdidload() { super.viewdidload() l.itemsize = cgsize(width: 400, height: 44) c = uicollectionview(frame: cgrectzero, collectionviewlayout: l) c.delegate = self c.datasource = self c.registerclass(mycollectionviewcell.self, forcellwithreuseidentifier: "cellid") view.addsubview(c) } override func viewdidlayoutsubviews() { super.viewdidlayoutsubviews() c.frame = view.bounds } func numberofsectionsincollectionview(collectionview: uicollectionview) -> int { return 1 } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 100 } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier("cellid", forindexpath: indexpath) as! mycollectionviewcell if cell.contentview.subviews.count == 0 { let label = uilabel(frame: cgrectzero) label.text = "row: \(indexpath.row)" cell.contentview.addsubview(label) label.frame = cell.contentview.bounds cell.contentview.backgroundcolor = uicolor.whitecolor() } return cell } }
here's video: http://cl.ly/1z3n2s2j0k3t
sorry crappy formatting, mom called , dinner's ready.
Comments
Post a Comment