Recently, because of the system upgrade caused the Xcode6. Series version of the bug, so began to use Xcode7. In the use of a sudden thought CollectionView in the iOS9 released a can move the cell's new features , try to implement it, but the API document interface can not be viewed, only some of the column API is placed there. So the internet search, found no such articles in the country, so FQ continue to find, eventually found are all swift version, so convert it to OC version to help domestic needs of friends learning to use. Here are the specific uses:
1. Create a CollectionView and set up an agent
-(Uicollectionview *) collectionview{if(_collectionview = =Nil) {Uicollectionviewflowlayout*layout =[[Uicollectionviewflowlayout alloc] init]; Layout.itemsize= Cgsizemake ( -, -); _collectionview= [[Uicollectionview alloc] Initwithframe:cgrectmake (0, -, Self.view.bounds.size.width, Self.view.bounds.size.height) collectionviewlayout:layout]; Layout.minimumlinespacing=Ten; Layout.minimuminteritemspacing=Ten; [_collectionview Registerclass:[uicollectionviewcellclass] Forcellwithreuseidentifier:@"Cell"]; _collectionview.backgroundcolor=[Uicolor Cyancolor]; _collectionview.datasource=Self ; //Add a long press gesture here to trigger the cell move effect with this gestureUilongpressgesturerecognizer *longgesture =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (handlelonggesture:)]; [_collectionview Addgesturerecognizer:longgesture]; } return_collectionview;}
2. Set up its resources
_datasource = [Nsmutablearray array]; for (int1; i++) { *imagename = [NSString stringWithFormat:@ "%d ", I]; [_datasource addobject:imagename]; }
3. Listen for gestures and set it to allow mobile cell and swap resources
- (void) Handlelonggesture: (Uilongpressgesturerecognizer *) Longgesture {//Judging gesture Status Switch(longgesture.state) { Caseuigesturerecognizerstatebegan:{//determine if the position of the gesture placement is on the pathNsindexpath *indexpath =[Self.collectionview indexpathforitematpoint:[longgesture LocationInView:self.collectionView]; if(Indexpath = =Nil) { Break; } //On the path, start moving the cell on the path[Self.collectionview Begininteractivemovementforitematindexpath:indexpath]; } Break; Caseuigesturerecognizerstatechanged://Update cell location at any time during the move[Self.collectionview updateinteractivemovementtargetposition:[longgesture locationInView:self.collectionVi EW]]; Break; Caseuigesturerecognizerstateended://Turn off cell movement after the move is over[Self.collectionview endinteractivemovement]; Break; default: [Self.collectionview cancelinteractivemovement]; Break; }}-(BOOL) CollectionView: (Uicollectionview *) CollectionView Canmoveitematindexpath: (Nsindexpath *) indexpath{//returns YES to allow its item to move returnYES;}- (void) CollectionView: (Uicollectionview *) CollectionView Moveitematindexpath: (Nsindexpath *) Sourceindexpath ToIndexPath :(nsindexpath*) Destinationindexpath {//Move the resource location of item and target location in an array of Exchange resources[_datasource ExchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];}
You can move the cell successfully with the above settings
At this point CollectionView's new features are used to show the completion, such as what is wrong in the place to look, thank you!
IOS9 CollectionView New Features