iOS學習,ios學習路線

來源:互聯網
上載者:User

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 上,點我

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.