0

ドラッグ アンド ドロップでセル値を交換するアプリに UILongGesture を完全に実装しました。今のところ、最初の行を最後の行と一緒に移動する場合、最初の行は最初の位置にとどまる必要があるという要件があります。つまり、位置を変更したくないということです。

コードのチャンクを試して時間を無駄にしましたが、結果を得ることができませんでした。以下は私のコードです。

- (IBAction)longPressGestureRecognized:(id)sender{

UILongPressGestureRecognizer *longGesture = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longGesture.state;
CGPoint location = [longGesture locationInView:self.tblTableView];
NSIndexPath *indexpath = [self.tblTableView indexPathForRowAtPoint:location];

static UIView *snapshotView = nil;
static NSIndexPath *sourceIndexPath = nil;

switch (state) {
    case UIGestureRecognizerStateBegan:
        if (indexpath) {
            sourceIndexPath = indexpath;
            UITableViewCell *cell = [self.tblTableView cellForRowAtIndexPath:indexpath];
            snapshotView = [self customSnapshotFromView:cell];
            __block CGPoint center = cell.center;
            snapshotView.center = center;
            snapshotView.alpha = 0.0;
            [self.tblTableView addSubview:snapshotView];
            [UIView animateWithDuration:0.25 animations:^{

                center.y = location.y;
                snapshotView.center = center;
                snapshotView.transform = CGAffineTransformMakeScale(1.05, 1.05);
                snapshotView.alpha = 0.98;

                cell.alpha = 0.0;

            } completion:^(BOOL finished) {
                cell.hidden = YES;
            }];
        }
        break;

    case UIGestureRecognizerStateChanged: {
        CGPoint center = snapshotView.center;
        center.y = location.y;
        snapshotView.center = center;

        if (indexpath && ![NSIndexPath isEqual:sourceIndexPath]) {

    [self.namesArray exchangeObjectAtIndex:indexpath.row withObjectAtIndex:sourceIndexPath.row];

            [self.tblTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexpath];

            sourceIndexPath = indexpath;

            NSIndexPath *indexPathOfLastItem =[NSIndexPath indexPathForRow:([self.namesArray count] - 1) inSection:0];
            NSLog(@"last :::: %@",indexPathOfLastItem);

            if (indexpath==indexPathOfLastItem) {
                [self.namesArray exchangeObjectAtIndex:indexPathOfLastItem.row withObjectAtIndex:sourceIndexPath.row];
                [self.tblTableView moveRowAtIndexPath:indexPathOfLastItem toIndexPath:0];

                UITableViewCell *cell = [self.tblTableView cellForRowAtIndexPath:sourceIndexPath];
                cell.hidden = NO;
                cell.alpha = 0.0;
            }
        }
        break;
    }

    default: {
        UITableViewCell *cell = [self.tblTableView cellForRowAtIndexPath:sourceIndexPath];
        cell.hidden = NO;
        cell.alpha = 0.0;

        [UIView animateWithDuration:0.25 animations:^{

            snapshotView.center = cell.center;
            snapshotView.transform = CGAffineTransformIdentity;
            snapshotView.alpha = 0.0;
            cell.alpha = 1.0;

        } completion:^(BOOL finished) {

            sourceIndexPath = nil;
            [snapshotView removeFromSuperview];
            snapshotView = nil;

        }];

        break;
    }
}
}

編集:私が遭遇したのは、セルが交換されていないことです。それは私が欲しいものですが、隠されています。ここに画像があります:Image1Image2

4

2 に答える 2

0

まず、行交換はUIGestureRecognizerStateChangedではなく で行うべきだと思いますUIGestureRecognizerStateEndedUIGestureRecognizerStateChanged画面を長押しして指を動かしている間、すばやく(何度も)処理されます。したがって、これにより行交換コードが何度も実行されますが、これはあなたの意図ではないと思います。長押しごとに1回実行されますUIGestureRecognizerStateBeganUIGestureRecognizerStateEnded

これらの次の 3 行のコードを保持しUIGestureRecognizerStateChanged、残りを に移動しUIGestureRecognizerStateEndedます。

CGPoint center = snapshotView.center;
center.y = location.y;
snapshotView.center = center;

s は次のUIGestureRecognizerStateとおりです。

UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible

switchsを使用する代わりにdefault:、ステート マシン ロジックでこれらの状態をより多くカバーする方がよいと思います。

于 2015-05-09T13:52:26.087 に答える
0

アニメーション中に指が画面から離れると、iOS は に戻りますgesture.state = UIGestureRecognizerStatePossible

そのため、そのジェスチャーを処理することを忘れないでください!

于 2015-10-29T13:16:07.410 に答える