-
longPress로 테이블 셀 이동하기👻 iOS 2015. 3. 2. 18:18
UITableViewCell을 longPress를 이용해서 순서를 바꾸는 방법을 정리
- tableView에 longPress recognizer를 추가한다.
- longPress의 상태에 따라 동작을 실행한다.
- 공통 : 제스쳐의 위치값을 가져와 선택한 셀이 무엇인지 얻어온다.
- Began : 선택한 셀을 복사해서 화면에 노출시킨 귀 본래의 셀은 hidden 상태로 바꾼다.
- Changed : tableView의 moveRowAtIndexPath 메소드를 이용해 위치를 바꿔준다.
- default : 복제된 뷰를 제거하고 본래의 셀의 hidden 상태를 해제한다.
- 완료
class TableReorderViewController: UITableViewController { var data: [Int] = [] override func viewDidLoad() { super.viewDidLoad() data = [ 1,2,3,4,5,6,7,9,10, 11,12,13,14,15,16,17,19,20, 21,22,23,24,25,26,27,29,30, 31,32,33,34,35,36,37,39,40 ] self.tableView.allowsSelection = false var longPress = UILongPressGestureRecognizer(target: self, action: "longPressGesture:") self.view.addGestureRecognizer(longPress) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell let entry = data[indexPath.row] cell.textLabel?.text = "#\(entry)" return cell } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } // longPress move private var sourceIndexPath: NSIndexPath! private var cellSnapshot: UIView! func longPressGesture(sender: UILongPressGestureRecognizer) { let point = sender.locationInView(self.tableView) if let indexPath = self.tableView.indexPathForRowAtPoint(point) { if let cell = self.tableView.cellForRowAtIndexPath(indexPath) { switch (sender.state) { case .Began: sourceIndexPath = indexPath cellSnapshot = self.customSnapshotFromView(cell) self.view.addSubview(cellSnapshot) cellSnapshot.center = cell.center cellSnapshot.alpha = 0.0 UIView.animateWithDuration(0.2, animations: { () -> Void in self.cellSnapshot.alpha = 0.9 self.cellSnapshot.transform = CGAffineTransformMakeScale(1.05, 1.05) }, completion: { (flag) -> Void in cell.hidden = true }) case .Changed: self.tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath: indexPath) sourceIndexPath = indexPath var center = cellSnapshot.center center.y = point.y cellSnapshot.center = center default: cell.alpha = 0.0 cell.hidden = false UIView.animateWithDuration(0.2, animations: { () -> Void in self.cellSnapshot.transform = CGAffineTransformIdentity self.cellSnapshot.center = cell.center self.cellSnapshot.alpha = 0.0 cell.alpha = 1.0 }, completion: { (flag) -> Void in cell.alpha = 1.0 self.cellSnapshot.removeFromSuperview() }) } } } } private func customSnapshotFromView(sourceView: UIView) -> UIView { UIGraphicsBeginImageContextWithOptions(sourceView.bounds.size, false, 0) sourceView.layer.renderInContext(UIGraphicsGetCurrentContext()) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() let snapshot = UIImageView(image: image) snapshot.layer.cornerRadius = 0.0 snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0) snapshot.layer.shadowRadius = 3.5 snapshot.layer.shadowOpacity = 0.3 return snapshot } }