ios - how to swipe cells in uitableview left and right, showing an image on left and an image on right -
as illustrated, need achieve when swiping left, button image shows up, blue one, , when swiping right green button shows up, how this? use swift , xcode 6.4
this tried before asking, able show 2 options text in right of cell, don't want that, needed in illustration, , said, buttons need images not text.
you can subclass uitableviewcell
incorporate uipangesturerecognizer
manipulates cell's contentview
s frame , add buttons behind contentview
.
to see how can work detail, added example code on how below reference. adds tap gesture recognizer 'close' action on tap instead of selecting cell.
also, requested in comments, here gif of how works (showing colors of buttons on side indication of action, can modify contentview
's frame overlapping buttons in subclass.)
// // mwswipeabletableviewcell.swift // mw ui toolkit // // created jan greve on 02.12.14. // copyright (c) 2014 markenwerk gmbh. rights reserved. // import uikit protocol mwswipeabletableviewcelldelegate : nsobjectprotocol { func swipeabletableviewcelldidrecognizeswipe(cell : mwswipeabletableviewcell) func swipeabletableviewcelldidtapleftbutton(cell : mwswipeabletableviewcell) func swipeabletableviewcelldidtaprightbutton(cell : mwswipeabletableviewcell) } class mwswipeabletableviewcell: uitableviewcell { weak var delegate : mwswipeabletableviewcelldelegate? var animationoptions : uiviewanimationoptions = [.allowuserinteraction, .beginfromcurrentstate] var animationduration : nstimeinterval = 0.5 var animationdelay : nstimeinterval = 0 var animationspingdamping : cgfloat = 0.5 var animationinitialvelocity : cgfloat = 1 private weak var leftwidthconstraint : nslayoutconstraint! private weak var rightwidthconstraint : nslayoutconstraint! var buttonwidth :cgfloat = 80 { didset(val) { if let r = self.rightwidthconstraint { r.constant = self.buttonwidth } if let l = self.leftwidthconstraint { l.constant = self.buttonwidth } } } private weak var panrecognizer : uipangesturerecognizer! private weak var buttoncanceltap : uitapgesturerecognizer! private var beginpoint : cgpoint = cgpointzero weak var rightbutton : uibutton! { willset(val) { if let r = self.rightbutton { r.removefromsuperview() } if let b = val { self.addsubview(b) b.addtarget(self, action: "didtapbutton:", forcontrolevents: .touchupinside) b.translatesautoresizingmaskintoconstraints = false self.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-(0)-[v]-(0)-|", options: [], metrics: nil, views: ["v":b])) self.addconstraints(nslayoutconstraint.constraintswithvisualformat("[v]-(0)-|", options: [], metrics: nil, views: ["v":b])) let wc = nslayoutconstraint(item: b, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1, constant: self.buttonwidth) b.addconstraint(wc) self.rightwidthconstraint = wc self.sendsubviewtoback(b) } } } weak var leftbutton : uibutton! { willset(val) { if let l = self.leftbutton { l.removefromsuperview() } if let b = val { self.addsubview(b) b.addtarget(self, action: "didtapbutton:", forcontrolevents: .touchupinside) b.translatesautoresizingmaskintoconstraints = false self.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|-(0)-[v]-(0)-|", options: [], metrics: nil, views: ["v":b])) self.addconstraints(nslayoutconstraint.constraintswithvisualformat("|-(0)-[v]", options: [], metrics: nil, views: ["v":b])) let wc = nslayoutconstraint(item: b, attribute: nslayoutattribute.width, relatedby: nslayoutrelation.equal, toitem: nil, attribute: nslayoutattribute.notanattribute, multiplier: 1, constant: self.buttonwidth) b.addconstraint(wc) self.leftwidthconstraint = wc self.sendsubviewtoback(b) } } } override func awakefromnib() { super.awakefromnib() } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) commoninit() } override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) commoninit() } private func commoninit() { let pan = uipangesturerecognizer(target: self, action: "didpan:") pan.delegate = self self.addgesturerecognizer(pan) self.panrecognizer = pan let tap = uitapgesturerecognizer(target: self, action: "didtap:") tap.delegate = self self.addgesturerecognizer(tap) self.buttoncanceltap = tap self.contentview.backgroundcolor = uicolor.clearcolor() } override func gesturerecognizershouldbegin(gesturerecognizer: uigesturerecognizer) -> bool { if let tap = gesturerecognizer as? uitapgesturerecognizer { if tap == self.buttoncanceltap { return self.contentview.frame.origin.x != 0 } else { return super.gesturerecognizershouldbegin(gesturerecognizer) } } else if let pan = gesturerecognizer as? uipangesturerecognizer { let trans = pan.translationinview(self) if abs(trans.x) > abs(trans.y) { return true } else if self.contentview.frame.origin.x != 0 { return true } else { return false } } else { return super.gesturerecognizershouldbegin(gesturerecognizer) } } func didtap(sender : uitapgesturerecognizer) { uiview.animatewithduration(self.animationduration, delay: self.animationdelay, usingspringwithdamping: self.animationspingdamping, initialspringvelocity: self.animationinitialvelocity, options: self.animationoptions, animations: { () -> void in self.contentview.frame.origin.x = 0 }, completion: nil) } func didpan(sender: uipangesturerecognizer) { switch sender.state { case .began: self.delegate?.swipeabletableviewcelldidrecognizeswipe(self) self.beginpoint = sender.locationinview(self) self.beginpoint.x -= self.contentview.frame.origin.x case .changed: let = sender.locationinview(self) let distx = now.x - self.beginpoint.x if distx <= 0 { let d = max(distx,-(self.contentview.frame.size.width-self.buttonwidth)) if d > -self.buttonwidth*2 || self.rightbutton != nil || self.contentview.frame.origin.x > 0 { self.contentview.frame.origin.x = d } else { sender.enabled = false sender.enabled = true } } else { let d = min(distx,self.contentview.frame.size.width-self.buttonwidth) if d < self.buttonwidth*2 || self.leftbutton != nil || self.contentview.frame.origin.x < 0 { self.contentview.frame.origin.x = d } else { sender.enabled = false sender.enabled = true } } default: delegate?.swipeabletableviewcelldidrecognizeswipe(self) let offset = self.contentview.frame.origin.x if offset > self.buttonwidth && self.leftbutton != nil { uiview.animatewithduration(self.animationduration, delay: self.animationdelay, usingspringwithdamping: self.animationspingdamping, initialspringvelocity: self.animationinitialvelocity, options: self.animationoptions, animations: { () -> void in self.contentview.frame.origin.x = self.buttonwidth }, completion: nil) } else if -offset > self.buttonwidth && self.rightbutton != nil { uiview.animatewithduration(self.animationduration, delay: self.animationdelay, usingspringwithdamping: self.animationspingdamping, initialspringvelocity: self.animationinitialvelocity, options: self.animationoptions, animations: { () -> void in self.contentview.frame.origin.x = -self.buttonwidth }, completion: nil) } else { uiview.animatewithduration(self.animationduration, delay: self.animationdelay, usingspringwithdamping: self.animationspingdamping, initialspringvelocity: self.animationinitialvelocity, options: self.animationoptions, animations: { () -> void in self.contentview.frame.origin.x = 0 }, completion: nil) } } } func closebuttonsifshown(animated:bool = true) -> bool { if self.contentview.frame.origin.x != 0 { if animated { uiview.animatewithduration(self.animationduration, delay: self.animationdelay, usingspringwithdamping: self.animationspingdamping, initialspringvelocity: self.animationinitialvelocity, options: self.animationoptions, animations: { () -> void in self.contentview.frame.origin.x = 0 self.panrecognizer.enabled = false self.panrecognizer.enabled = true }, completion: nil) } else { self.contentview.frame.origin.x = 0 self.panrecognizer.enabled = false self.panrecognizer.enabled = true } return true } else { return false } } func didtapbutton(sender:uibutton!) { if let d = delegate { if let l = self.leftbutton { if sender == l { d.swipeabletableviewcelldidtapleftbutton(self) } } if let r = self.rightbutton { if sender == r { d.swipeabletableviewcelldidtaprightbutton(self) } } } self.closebuttonsifshown(false) } override func sethighlighted(highlighted: bool, animated: bool) { let showing = self.contentview.frame.origin.x != 0 if !showing { super.sethighlighted(highlighted, animated: animated) self.rightbutton?.alpha = showing || !highlighted ? 1 : 0 self.leftbutton?.alpha = showing || !highlighted ? 1 : 0 } } override func setselected(selected: bool, animated: bool) { let showing = self.contentview.frame.origin.x != 0 if !showing { super.setselected(selected, animated: animated) self.rightbutton?.alpha = showing || !selected ? 1 : 0 self.leftbutton?.alpha = showing || !selected ? 1 : 0 } } }
Comments
Post a Comment