IOS learning and ios learning routes
There are 100 items and there are only 20 data sources. They can only be moved between 20 items to prevent item reuse and cause bugs.
Method 1: Apple comes
//UICollectionViewDataSource
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath;- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;
Method 2:
1. Get the Item to be dragged
2. Use the built-in method to hide the current Item
3. Switch location and Data Location
# Pragma mark -- gesture start-(void) xwp_gestureBegan :( UILongPressGestureRecognizer *) longPressGesture {CGFloat y = [longPressGesture locationInView: longPressGesture. view]. y; // if (y <= [self row]) is not executed for items that exceed the data source {// obtain the cell _ originalIndexPath = [self indexPathForItemAtPoint: [longPressGesture locationOfTouch: 0 inView: longPressGesture. view]; UICollectionViewCell * cell = [self cellForItemAtIndexPath: _ or IginalIndexPath]; // use the built-in method UIView * tempMoveCell = [cell snapshotViewAfterScreenUpdates: NO]; // hide the current Item cell. hidden = YES; _ tempMoveCell = tempMoveCell; _ tempMoveCell. frame = cell. frame; [self addSubview: _ tempMoveCell]; // enable the edge scroll timer [self xwp_setEdgeTimer]; // enable the jitter if (_ shakeWhenMoveing &&! _ Editing) {[self xwp_shakeAllCell]; [self addObserver: self forKeyPath: @ "contentOffset" options: encryption context: nil];} _ lastPoint = [longPressGesture locationOfTouch: 0 inView: longPressGesture. view]; // notification proxy if ([self. delegate respondsToSelector: @ selector (dragCellCollectionView: cellWillBeginMoveAtIndexPath :)]) {[self. delegate dragCellCollectionView: self cellWillBeginMoveAtIndexPath: _ originalIndexPath] ;}}
# Pragma mark -- gesture jitter-(void) xwp_gestureChange :( UILongPressGestureRecognizer *) longPressGesture {// obtain the position y value CGFloat y = [longPressGesture locationInView: longPressGesture. view]. y; // specifies the range of gesture values. if (y <= [self row]) {NSLog (@ "----- % f", y ); // notification proxy if ([self. delegate respondsToSelector: @ selector (dragCellCollectionViewCellisMoving :)]) {[self. delegate dragCellCollectionViewCellisMoving: self];} CGFloat tranX = [longPressGesture locationOfTouch: 0 inView: longPressGesture. view]. x-_ lastPoint. x; CGFloat tranY = [longPressGesture locationOfTouch: 0 inView: longPressGesture. view]. y-_ lastPoint. y; _ tempMoveCell. center = CGPointApplyAffineTransform (_ tempMoveCell. center, CGAffineTransformMakeTranslation (tranX, tranY); _ lastPoint = [longPressGesture locationOfTouch: 0 inView: longPressGesture. view]; [self xwp_moveCell];} else {// if the range is exceeded, set the value of item y to the bottom of the data source _ tempMoveCell. frame = CGRectMake (_ tempMoveCell. frame. origin. x, [self row], _ tempMoveCell. frame. size. width, _ tempMoveCell. frame. size. height );}}
# Pragma mark -- cancel or end with a gesture-(void) xwp_gestureEndOrCancle :( handle *) longPressGesture {UICollectionViewCell * cell = [self cellForItemAtIndexPath: _ originalIndexPath]; self. userInteractionEnabled = NO; [self xwp_stopEdgeTimer]; // notify the proxy if ([self. delegate respondsToSelector: @ selector (dragCellCollectionViewCellEndMoving :)]) {[self. delegate dragCellCollectionViewCellEndMoving: self];} // a small animation [UIView animateWithDuration: 0.25 animations: ^ {_ tempMoveCell. center = cell. center;} completion: ^ (BOOL finished) {[self xwp_stopShakeAllCell]; [_ tempMoveCell removeFromSuperview]; cell. hidden = NO; self. userInteractionEnabled = YES;}];}
Complete demo, put it on githud, click me