iOS Learning-Uicollectionview mobile Item, similar to backpack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.