两套逻辑
privatefuncdragCell(cell:UITableViewCell?){
if#available(iOS11.0, *) {
cell?.userInteractionEnabledWhileDragging = true
}else{
letpan =UILongPressGestureRecognizer(target:self, action:#selector(self.longPressGesture))
cell?.addGestureRecognizer(pan)
}
}
//MARK: iOS 11以下 拖动实现
@objcfunclongPressGesture(_recognise:UILongPressGestureRecognizer) {
letcurrentPoint:CGPoint= recognise.location(in:tableView)
letcurrentIndexPath =tableView.indexPathForRow(at: currentPoint)
guardletindexPath = currentIndexPathelse{
initCellImageView()
return
}
guardindexPath.row
initCellImageView()
return
}
switchrecognise.state{
case.began:
longPressGestureBegan(recognise)
break
case.changed:
longPressGestureChanged(recognise)
break
default:
self.touchPoints.removeAll()
ifletcell =tableView.cellForRow(at:sourceIndexPath! ){
cell.isHidden=false
}
guardletrouteLine =self.routeLineModelelse{return}
vartempArr:[CRPointsModel] = [CRPointsModel]()
forlineinrouteLine {
iflineisCRPointsModel{
tempArr.append(lineas!CRPointsModel)
}
}
(self.parentas?CREditTripMapViewController)?.addLine(routeLineArr: tempArr)
initCellImageView()
break
}
}
privatefunclongPressGestureBegan(_recognise:UILongPressGestureRecognizer) {
self.gestureRecognizer= recognise
letcurrentPoint:CGPoint= recognise.location(in:tableView)
letcurrentIndexPath =tableView.indexPathForRow(at: currentPoint)
if(currentIndexPath !=nil) {
sourceIndexPath= currentIndexPath
currentCell=tableView.cellForRow(at: currentIndexPath! )as!CRTripListCell
cellImageView = getImageView(currentCell)
cellImageView.frame = currentCell.frame
tableView.addSubview(cellImageView)
self.currentCell.isHidden=true
}
}
privatefunclongPressGestureChanged(_recognise:UILongPressGestureRecognizer) {
letselectedPoint:CGPoint= recognise.location(in:tableView)
varselectedIndexPath =tableView.indexPathForRow(at: selectedPoint)
ifselectedIndexPath ==nil|| selectedIndexPath?.row==0{
///第一个cell 不可以移动
selectedIndexPath =IndexPath(row:1, section:0)
}
if tableView.numberOfSections == 1 {
self.touchPoints.append(selectedPoint)
ifself.touchPoints.count>2{
self.touchPoints.remove(at:0)
}
varcenter =cellImageView.center
center.y= selectedPoint.y
// 快照随触摸点x值改变量移动
letPpoint =self.touchPoints.first
letNpoint =self.touchPoints.last
letmoveX = Npoint!.x- Ppoint!.x
center.x+= moveX
cellImageView.center= center
if((selectedIndexPath !=nil) && selectedIndexPath !=sourceIndexPath) {
tableView.beginUpdates()
letcellmode =routeLineModel![sourceIndexPath!.row]
objc_sync_enter(self)
self.routeLineModel!.remove(at: sourceIndexPath!.row)
ifselectedIndexPath!.row
self.routeLineModel!.insert(cellmode, at: selectedIndexPath!.row)
}else{
self.routeLineModel!.append(cellmode)
}
self.mapDrawingLine(model: self.sortCRJourneyDetailModel(model: self.routeLineModel))
objc_sync_exit(self)
self.tableView.moveRow(at:sourceIndexPath!, to: selectedIndexPath!)
tableView.endUpdates()
sourceIndexPath= selectedIndexPath
}
}
}
privatefuncinitCellImageView() {
self.cellImageView.alpha = 0.0
self.cellImageView=UIImageView()
self.cellImageView.removeFromSuperview()
tableView.reloadData()
}
// MARK: - 截图
privatefuncgetImageView(_cell:UITableViewCell) ->UIImageView{
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, false, 0)
cell.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
letimageView =UIImageView(image: image)
returnimageView
}
//MARK: iOS11 拖动实现
extension CRAddTripListController:UITableViewDragDelegate,UITableViewDropDelegate{
@available(iOS 11.0, *)
functableView(_tableView:UITableView, itemsForBeginning session:UIDragSession, at indexPath:IndexPath) -> [UIDragItem] {
letcellmode =self.routeLineModel![indexPath.row]
ifcellmodeisCRPointsModel{
letimage =UIImage()
letprovider =NSItemProvider(object: image)
letitem =UIDragItem(itemProvider: provider)
return[item]
}else{
return[]
}
}
// MARK: UITableViewDropDelegate
@available(iOS 11.0, *)
functableView(_tableView:UITableView, performDropWith coordinator:UITableViewDropCoordinator) {
}
@available(iOS 11.0, *)
functableView(_tableView:UITableView, dropSessionDidUpdate session:UIDropSession, withDestinationIndexPath destinationIndexPath:IndexPath?) ->UITableViewDropProposal{
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
@available(iOS 11.0, *)
functableView(_tableView:UITableView, canHandle session:UIDropSession) ->Bool{
// Only receive image data
returnsession.canLoadObjects(ofClass:UIImage.self)
}
}