Ios-tableview Drag Cell Change Order

Source: Internet
Author: User

Effect:

Long pressing a cell,cell will have an obvious bounce-up effect. At this point, you can change the order by dragging the cell and other cells.

The principle of implementation:

1, floating layer

When you press and hold, a floating layer appears on the UITableViewCell, while the UITableViewCell is hidden. And the floating layer can be dragged. (Generate a snapshot of UITableViewCell)

1 #pragmaMark-helper methods2 3 /** @brief Returns A customized snapshot of a given view.*/4-(UIView *) Customsnapshofromview: (UIView *) Inputview {5uiview* snapshot =Nil;6     7     if([[[[Uidevice Currentdevice] systemversion] Doublevalue] <7.0) {8         //ios7.0 The following form to save the snapshot9snapshot =[self customsnapshortfromviewex:inputview];Ten}Else{ One         //snapshot methods for ios7.0 systems Asnapshot =[Inputview Snapshotviewafterscreenupdates:yes]; -     } -      theSnapshot.layer.masksToBounds =NO; -Snapshot.layer.cornerRadius =0.0; -Snapshot.layer.shadowOffset = Cgsizemake (-5.0,0.0); -Snapshot.layer.shadowRadius =5.0; +Snapshot.layer.shadowOpacity =0.4; -      +     returnsnapshot; A } at  --(UIView *) Customsnapshortfromviewex: (UIView *) Inputview - { -Cgsize insize =inputView.bounds.size; -     //The following method, the first parameter represents the size of the area. The second parameter indicates whether it is non-transparent. If you need to display a translucent effect, you need to pass no, otherwise pass yes. The third parameter is the screen density. - uigraphicsbeginimagecontextwithoptions (Insize, NO, [UIScreen mainscreen].scale); in [Inputview.layer Renderincontext:uigraphicsgetcurrentcontext ()]; -UIImage *image=Uigraphicsgetimagefromcurrentimagecontext (); to Uigraphicsendimagecontext (); +uiimageview* snapshot =[[Uiimageview alloc] initwithimage:image]; -      the     returnsnapshot; *}

2, replace the position of a UITableViewCell

?
1 2 3 4 // ... update data source. [_items exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];              // ... move the rows.[_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

The location of the data inside the datasource is replaced, and the cell location of the TableView is replaced.

1- (void) Longpressgesturerecognized: (ID) Sender {2     3Uilongpressgesturerecognizer *longpress = (Uilongpressgesturerecognizer *) sender;4Uigesturerecognizerstate State =longpress.state;5     6Cgpoint location =[longpress Locationinview:_tableview];7Nsindexpath *indexpath =[_tableview indexpathforrowatpoint:location];8     9     StaticUIView *snapshot = nil;///< A snapshot of the row user is moving.Ten static Nsindexpath *sourceindexpath = nil;///< Initial index path, where gesture begins. One      A     Switch(state) { -          CaseUigesturerecognizerstatebegan: { -             if(Indexpath) { theSourceindexpath =Indexpath; -                  -UITableViewCell *cell =[_tableview Cellforrowatindexpath:indexpath]; -                  +                 //Take a snapshot of the selected row using helper method. -snapshot =[self Customsnapshofromview:cell]; +                  A                 //Add the snapshot as Subview, centered at cell ' s center ... at__block Cgpoint Center =Cell.center; -Snapshot.center =Center; -Snapshot.alpha =0.0; - [_tableview addsubview:snapshot]; -[UIView animatewithduration:0.25animations:^{ -                      in                     //Offset for gesture location. -CENTER.Y =LOCATION.Y; toSnapshot.center =Center; +Snapshot.transform = Cgaffinetransformmakescale (1.05,1.05); -Snapshot.alpha =0.98; the                      *Cell.alpha =0.0f; $} completion:^(BOOL finished) {Panax NotoginsengCell.hidden =YES; -                 }]; the             } +              Break; A         } the              +          Caseuigesturerecognizerstatechanged: { -Cgpoint Center =Snapshot.center; $CENTER.Y =LOCATION.Y; $Snapshot.center =Center; -              -             //is destination valid and was it different from source? the             if(Indexpath &&!)[Indexpath Isequal:sourceindexpath]) { -                 Wuyi                 //... update data source. the [_items exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row]; -                  Wu                 //... move the rows. - [_tableview Moverowatindexpath:sourceindexpath Toindexpath:indexpath]; About                  $                 //... and update source so it's in sync with UI changes. -Sourceindexpath =Indexpath; -             } -              Break; A         } +              the         default: { -             //Clean up . $UITableViewCell *cell =[_tableview Cellforrowatindexpath:sourceindexpath]; the[UIView animatewithduration:0.25animations:^{ the                  theSnapshot.center =Cell.center; theSnapshot.transform =cgaffinetransformidentity; -Snapshot.alpha =0.0; in                  theCell.alpha =1.0f; the} completion:^(BOOL finished) { AboutCell.hidden =NO; the [Snapshot Removefromsuperview]; thesnapshot =Nil; the                  +             }]; -Sourceindexpath =Nil; the              Break;Bayi         } the     } the}

3, ... Does it seem like there's nothing else?

You see, isn't it simple! With just a few lines of code above, you can achieve a slightly better-looking UITableViewCell transform position.

Download Demo

Related Article

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.