1. 程式人生 > >自定義tableview移動

自定義tableview移動

關於tableview的移動,apple提供了官方的做法,但是無法做擴充套件,生成的cell往往不符合實際需求,所以這裡提供自定義移動操作:


//為tableview新增對應的手勢來模擬移動手勢。
UILongPressGestureRecognizer* longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handlerLongPress:)];
longPressGesture.minimumPressDuration=0.5;
[_sortTableView addGestureRecognizer:longPressGesture];


-(void
)handlerLongPress:(UILongPressGestureRecognizer*)sender{ switch (sender.state) { case UIGestureRecognizerStateBegan: [self handlerLongPressBegan:sender]; break; case UIGestureRecognizerStateChanged: [self handlerLongPressChanged:sender]; break
; case UIGestureRecognizerStateCancelled: [self handlerLongPressEnded:sender]; break; case UIGestureRecognizerStateEnded: [self handlerLongPressEnded:sender]; break; default: break; } } -(void)handlerLongPressBegan:(UILongPressGestureRecognizer*)longPressGesture{ //手勢識別開始階段獲取對應的觸碰位置.
CGPoint location=[longPressGesture locationInView:_sortTableView]; NSIndexPath* indexPath=[_sortTableView indexPathForRowAtPoint:location]; //判斷觸碰位置是否為tableview的某一行 if(indexPath){ //_sourceIndexPath為屬性變數用於儲存初始被移動的cell的位置。 _sourceIndexPath=indexPath; //獲取cell QTShortcutsListenSortTableCell* cell=[_sortTableView cellForRowAtIndexPath:indexPath]; //對cell進行截圖並儲存到屬性變數 _snapshot=[cell resizableSnapshotViewFromRect:CGRectMake(0, 0, cell.frame.size.width, 50) afterScreenUpdates:NO withCapInsets:UIEdgeInsetsZero]; //得到初始位置的cell的中心。然後隱藏cell,並將截圖放到cell對應的位置,用此截圖來假裝一個cell來拖動。 CGPoint center=cell.center; [_sortTableView addSubview:_snapshot]; center.y=location.y; _snapshot.center=center; _snapshot.transform=CGAffineTransformMakeScale(1, 1); _snapshot.alpha=1; _snapshot.backgroundColor=cell.backgroundColor; cell.backgroundColor=[UIColor clearColor]; cell.hidden=true; } } -(void)handlerLongPressChanged:(UILongPressGestureRecognizer*)longPressGesture{ //當手勢改變的時候,獲取所處的位置,然後一步步交換截圖以及對應的cell和相對應的資料。 CGPoint location=[longPressGesture locationInView:_sortTableView]; NSIndexPath* indexPath=[_sortTableView indexPathForRowAtPoint:location]; CGPoint center=_snapshot.center; center.y=location.y; _snapshot.center=center; if(indexPath&&![indexPath isEqual:_sourceIndexPath]){ //這裡的_sortModelArr對應著資料模型陣列 QTShortcutsListenSortModel* modelToMove=_sortModelArr[_sourceIndexPath.row]; [_sortModelArr removeObjectAtIndex:_sourceIndexPath.row]; [_sortModelArr insertObject:modelToMove atIndex:indexPath.row]; [_sortTableView moveRowAtIndexPath:_sourceIndexPath toIndexPath:indexPath]; _sourceIndexPath=indexPath; } } -(void)handlerLongPressEnded:(UILongPressGestureRecognizer*)longPressGesture{ QTShortcutsListenSortTableCell* cell=[_sortTableView cellForRowAtIndexPath:_sourceIndexPath]; //手勢結束以後,將截圖置為nil,將原來的cell展示出來。 [UIView animateWithDuration:0.25 animations:^{ _snapshot.center=cell.center; _snapshot.transform=CGAffineTransformIdentity; _snapshot.alpha=0; cell.backgroundColor=_snapshot.backgroundColor; cell.hidden=false; } completion:^(BOOL finished) { [_snapshot removeFromSuperview]; _snapshot=nil; }]; _sourceIndexPath=nil; }