Method One: Apple comes with
Uicollectionviewdatasource
-(BOOL) CollectionView: (Uicollectionview *) CollectionView Canmoveitematindexpath: (Nsindexpath *) IndexPath; -(void) CollectionView: (Uicollectionview *) CollectionView Moveitematindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (nsindexpath*) Destinationindexpath;
Method Two:
1. Get the Item you want to drag
2. Use the system's own method to hide the current Item
3. Swap location, data location
#pragmaMark--The gesture begins--(void) Xwp_gesturebegan: (Uilongpressgesturerecognizer *) longpressgesture{CGFloat y=[Longpressgesture locationinview:longpressgesture.view].y; //item that exceeds the data source does not execute if(Y <=[self row]) { //get the cell where the finger resides_originalindexpath = [Self indexpathforitematpoint:[longpressgesture locationoftouch:0InView:longPressGesture.view]]; Uicollectionviewcell*cell =[self cellforitematindexpath:_originalindexpath]; //using the system's own methodUIView *tempmovecell =[Cell Snapshotviewafterscreenupdates:no]; //Hide Current ItemCell.hidden =YES; _tempmovecell=Tempmovecell; _tempmovecell.frame=Cell.frame; [Self Addsubview:_tempmovecell]; //Turn on edge scrolling timer[self xwp_setedgetimer]; //Turn on Jitter if(_shakewhenmoveing &&!)_editing) {[Self Xwp_shakeallcell]; [Self addobserver:self forkeypath:@"Contentoffset"Options:nskeyvalueobservingoptionnew Context:nil]; } _lastpoint= [Longpressgesture Locationoftouch:0InView:longPressGesture.view]; //Notification Agent if([Self.Delegaterespondstoselector: @selector (Dragcellcollectionview:cellwillbeginmoveatindexpath:)]) {[Self.Delegatedragcellcollectionview:self Cellwillbeginmoveatindexpath:_originalindexpath]; } }}
#pragmaMark--Gesture jitter-(void) Xwp_gesturechange: (Uilongpressgesturerecognizer *) longpressgesture{//get the click position y valueCGFloat y =[Longpressgesture locationinview:longpressgesture.view].y; //limit gesture Range y value if(Y <=[self row]) {NSLog (@"-----%f", y); //Notification Agent if([Self.Delegaterespondstoselector: @selector (dragcellcollectionviewcellismoving:)]) {[Self.DelegateDragcellcollectionviewcellismoving:self]; } cgfloat Tranx= [Longpressgesture Locationoftouch:0Inview:longpressgesture.view].x-_lastpoint.x; CGFloat Trany= [Longpressgesture Locationoftouch:0INVIEW:LONGPRESSGESTURE.VIEW].Y-_lastpoint.y; _tempmovecell.center=Cgpointapplyaffinetransform (_tempmovecell.center, Cgaffinetransformmaketranslation (TranX, TranY)); _lastpoint= [Longpressgesture Locationoftouch:0InView:longPressGesture.view]; [Self Xwp_movecell]; }Else{ //if the range is exceeded, the current dragged item Y value is set to the bottom of the data source_tempmovecell.frame =CGRectMake (_tempmovecell.frame.origin.x, [self row], _tempmovecell.frame.size.width, _ TempMoveCell.frame.size.height); }}
#pragmaMark--gesture cancellation or end-(void) Xwp_gestureendorcancle: (Uilongpressgesturerecognizer *) longpressgesture{Uicollectionviewcell*cell =[self cellforitematindexpath:_originalindexpath]; Self.userinteractionenabled=NO; [Self xwp_stopedgetimer]; //Notification Agent if([Self.Delegaterespondstoselector: @selector (dragcellcollectionviewcellendmoving:)]) {[Self.DelegateDragcellcollectionviewcellendmoving:self]; } //a small animation[UIView animatewithduration:0.25animations:^{_tempmovecell.center=Cell.center; } Completion:^(BOOL finished) {[Self Xwp_stopshakeallcell]; [_tempmovecell Removefromsuperview]; Cell.hidden=NO; Self.userinteractionenabled=YES; }];}
Complete demo, put on the Githud, point me
iOS Learning-Uicollectionview mobile Item, similar to backpack