iOS學習,ios學習路線
有100個 item,資料來源只有20個,只能在 20 個之間移動,防止 item 複用,出現 bug
方法一:蘋果內建
//UICollectionViewDataSource
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath;- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;
方法二:
1.擷取要拖拽的 Item
2.使用系統內建方法,隱藏當前 Item
3.交換位置、資料位元置
#pragma mark -- 手勢開始- (void)xwp_gestureBegan:(UILongPressGestureRecognizer *)longPressGesture{ CGFloat y = [longPressGesture locationInView:longPressGesture.view].y; //超過資料來源的 item 不執行 if (y <= [self row]) { //擷取手指所在的cell _originalIndexPath = [self indexPathForItemAtPoint:[longPressGesture locationOfTouch:0 inView:longPressGesture.view]]; UICollectionViewCell *cell = [self cellForItemAtIndexPath:_originalIndexPath]; //使用系統內建方法 UIView *tempMoveCell = [cell snapshotViewAfterScreenUpdates:NO]; //隱藏當前 Item cell.hidden = YES; _tempMoveCell = tempMoveCell; _tempMoveCell.frame = cell.frame; [self addSubview:_tempMoveCell]; //開啟邊緣滾動定時器 [self xwp_setEdgeTimer]; //開啟抖動 if (_shakeWhenMoveing && !_editing) { [self xwp_shakeAllCell]; [self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil]; } _lastPoint = [longPressGesture locationOfTouch:0 inView:longPressGesture.view]; //通知代理 if ([self.delegate respondsToSelector:@selector(dragCellCollectionView:cellWillBeginMoveAtIndexPath:)]) { [self.delegate dragCellCollectionView:self cellWillBeginMoveAtIndexPath:_originalIndexPath]; } }}
#pragma mark -- 手勢抖動- (void)xwp_gestureChange:(UILongPressGestureRecognizer *)longPressGesture{ //擷取點擊位置 y 值 CGFloat y = [longPressGesture locationInView:longPressGesture.view].y; //限制手勢範圍 y 值 if (y <= [self row]) { NSLog(@"-----%f",y); //通知代理 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{ //如果超過範圍,就把當前拖動的 item y 值設為資料來源的底部 _tempMoveCell.frame = CGRectMake(_tempMoveCell.frame.origin.x, [self row], _tempMoveCell.frame.size.width, _tempMoveCell.frame.size.height); }}
#pragma mark -- 手勢取消或者結束- (void)xwp_gestureEndOrCancle:(UILongPressGestureRecognizer *)longPressGesture{ UICollectionViewCell *cell = [self cellForItemAtIndexPath:_originalIndexPath]; self.userInteractionEnabled = NO; [self xwp_stopEdgeTimer]; //通知代理 if ([self.delegate respondsToSelector:@selector(dragCellCollectionViewCellEndMoving:)]) { [self.delegate dragCellCollectionViewCellEndMoving:self]; } //一個小動畫 [UIView animateWithDuration:0.25 animations:^{ _tempMoveCell.center = cell.center; } completion:^(BOOL finished) { [self xwp_stopShakeAllCell]; [_tempMoveCell removeFromSuperview]; cell.hidden = NO; self.userInteractionEnabled = YES; }];}
完整 demo,放在 githud 上,點我