tableview中的Long Press Gesture可提高应用内存利用率

时间:2016-02-15 11:50:38

标签: ios objective-c

当我连续使用长按表格视图来移动行时,似乎增加了记忆力,并且在某些时候行动变得困难之后感觉有些卡在中间,你是否知道高内存利用背后的原因是什么? ???

 -(IBAction)longPressGestureRecognized:(id)sender {


    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;

    CGPoint location = [longPress locationInView:self.ModeTableView];
    NSIndexPath *indexPath = [self.ModeTableView indexPathForRowAtPoint:location];

    static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.
    static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.

    switch (state) {
        case UIGestureRecognizerStateBegan: {
            if (indexPath) {
                sourceIndexPath = indexPath;
                UITableViewCell *cell = [self.ModeTableView cellForRowAtIndexPath:indexPath];
                // Take a snapshot of the selected row using helper method.
                snapshot = [self customSnapshoFromView:cell];
                // Add the snapshot as subview, centered at cell's center...
                __block CGPoint center = cell.center;
                snapshot.center = center;
                snapshot.alpha = 0.0;

                [self.ModeTableView addSubview:snapshot];
                [UIView animateWithDuration:0.10  animations:^{
                    // Offset for gesture location.
                    center.y = location.y;
                    snapshot.center = center;
                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapshot.alpha = 0.98;
                    cell.alpha = 0.0;
                } completion:^(BOOL finished) {
                    cell.hidden = YES;
                }];
            }
            break;
        }

        case UIGestureRecognizerStateChanged: {
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;
            if(sourceIndexPath.row<indexPath.row)
            {
                [UIView animateWithDuration: 0.5 animations: ^{
                    [ModeTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
                } completion: ^(BOOL finished){
                }];
            }
            else if(sourceIndexPath.row>indexPath.row){
                [UIView animateWithDuration: 0.5 animations: ^{
                    [ModeTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
                } completion: ^(BOOL finished){
                }];
            }

            // Is destination valid and is it different from source?
            if (indexPath&&indexPath!=sourceIndexPath) {
                // ... update data source.
                [modeTableData exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
                // ... move the rows.
                [self.ModeTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;

                NSInteger switchIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:@"SwitchSelection"] intValue];
                if(switchIndex==2)
                {
                    photoModeTableData=modeTableData;
                    [[NSUserDefaults standardUserDefaults] setObject:photoModeTableData forKey:@"PhotoModeData"];
                }
                else
                {
                    videoModeTableData=modeTableData;
                    [[NSUserDefaults standardUserDefaults] setObject:videoModeTableData forKey:@"VideoModeData"];
                }


                               }
            break;
        }
        case UIGestureRecognizerStateEnded:
        {

            if(indexPath.row==0||indexPath.row==1)
            {
                [self setCheckMarkForRow:indexPath.row];
                [self sendUpdatedModeDetails];

            }


        }
        default: {
            // Clean up.
            UITableViewCell *cell = [self.ModeTableView cellForRowAtIndexPath:sourceIndexPath];
            cell.hidden = NO;
            // cell.alpha = 0.0;
            [UIView animateWithDuration:0.10 animations:^{
                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                cell.alpha = 1.0;
            } completion:^(BOOL finished) {
                sourceIndexPath = nil;
                //[snapshot removeFromSuperview];
                snapshot = nil;
                [ModeTableView reloadData];
            }];

            break;
        }
    }
}
/** @brief Returns a customized snapshot of a given view. */
- (UIView *)customSnapshoFromView:(UIView *)inputView {
    // Make an image from the input view.
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Create an image view.
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;
    return snapshot;
}

1 个答案:

答案 0 :(得分:0)

您的代码问题是动画。如果要为行的移动设置动画,则需要执行以下操作:

tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)

代码中唯一优秀且有效的动画是最后和开始时的动画。