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而是应该在UIGestureRecognizerStateEnded. UIGestureRecognizerStateChanged当您长按并在屏幕上移动手指时,会快速处理(很多次)。所以这会导致行交换代码运行多次,我猜这不是你的意图。UIGestureRecognizerStateBegan并且UIGestureRecognizerStateEnded每次长按运行一次。

我将保留以下三行代码UIGestureRecognizerStateChanged并将其余代码移至UIGestureRecognizerStateEnded

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

UIGestureRecognizerStates如下:

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 回答