简体   繁体   中英

Long Press Gesture in tableview increase app memory utilisation

When i continuously uses long press on table view to move rows, it seems like increases memory utlisation and after sometime row movement become difficult feel like some stuck in between, Do you have any idea what is the reason behind the high memory utilisation???

 -(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;
}

The problem of your code are the Animations. If you want to animate the movement of your rows you need to do this:

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

The only good and working animation in your code is the animation at the end and at the start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM