Question

I have a table view that I would like to scroll when a selected a cell and "pop" it out and move it around the table view. The table view scrolls just fine and the long press to select a cell works, but I'm not able to scroll the table view when I have a cell selected.

I read up on UIGestureRecognizerDelegate but I'm not sure that is making a difference. I also call these method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Here is part of my code:

- (IBAction)longPressGestureRecognized:(id)sender {

UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
//longPress.delegate = self;

CGPoint location = [longPress locationInView:self.personTableView];
NSIndexPath *indexPath = [self.personTableView 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.personTableView cellForRowAtIndexPath:indexPath];
            // capture the color of the cell before blacking out
            savedTextColor = cell.detailTextLabel.textColor;

            // Take a snapshot of the selected row using helper method.
            snapshot = [self customSnapshotFromView:cell];

            // Add the snapshot as subview, centered at cell's center...
            __block CGPoint center = cell.center;
            snapshot.center = center;
            snapshot.alpha = 0.0;
            [self.personTableView addSubview:snapshot];
            [UIView animateWithDuration:0.25 animations:^{

                // Offset for gesture location.
                center.y = location.y;
                snapshot.center = center;
                snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                snapshot.alpha = 0.98;

                // Black out.
                cell.detailTextLabel.alpha = 0;
                cell.textLabel.alpha = 0;
            } completion:nil];
        }
        break;
    }
    case UIGestureRecognizerStateChanged: {
        CGPoint center = snapshot.center;
        center.y = location.y;
        snapshot.center = center;

        // Is destination valid and is it different from source?
        if (indexPath && ![indexPath isEqual:sourceIndexPath]) {

            // ... update data source.
            [dataSingelton saveUpdatedPersonList:(int)indexPath.row sourceIndex:(int)sourceIndexPath.row];
            //[[dataSingelton mutableDataArray] exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

            // ... move the rows.
            [self.personTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

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

    default: {
        // Clean up.
        UITableViewCell *cell = [self.personTableView cellForRowAtIndexPath:sourceIndexPath];
        [UIView animateWithDuration:0.25 animations:^{
            snapshot.center = cell.center;
            snapshot.transform = CGAffineTransformIdentity;
            snapshot.alpha = 0.1;
            cell.detailTextLabel.alpha = 1;
            cell.textLabel.alpha = 1;
        } completion:^(BOOL finished) {
            [snapshot removeFromSuperview];
            snapshot = nil;
            cell.hidden = NO;
        }];
        sourceIndexPath = nil;
        break;
    }
}

}

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}



- (UIView *)customSnapshotFromView:(UIView *)inputView {

    UIView *snapshot = [inputView snapshotViewAfterScreenUpdates:YES];
    snapshot.alpha = 0.1;
    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;
}

When I set the delegate to self like this:

longPress.delegate = self;

The able view scrolls, but then my selected cell doesn't move. What am I missing to make this work?

* Updated * After doing some more reading/research I think in order for me to achieve the effect I want (the scrolling effect on the TableView in Apple's weather app) I think that my long press gesture will also have to handle the scrolling of my table view. The default scrolling starts to scroll the entire table instantly, where as I need the table to scroll only when the long pressed cell is at the top or the bottom of my TableView. Am I correct in thinking like this? I've been trying to find an example to break down and study, but haven't had any luck finding anything like that.

** Updated ** So I'm trying to figure out how to trigger the scroll up timer when the user drags a cell up towards the top. The problem I'm having right now is figuring out when that cell is close to the top, when its at the very top of the tableview, thats easy I could use something like tableViewHight + 100, but this code wont work when the tableview is scrolled down. Here is what I have so far:

NSLog(@"This is table view hight %.2f", tableViewHight);

            NSLog(@"This is content offset %f",  (personTableView.contentOffset.y));

            NSLog(@"Should be 100 place holder %.2f", (tableViewHight - tableViewHight) + personTableView.contentOffset.y + 100);

            NSLog(@"location y %f", location.y);



            // gets called repeatedly
            // Step 1: check to see if we need to start scrolling
            if (location.y < tableViewHight - tableViewHight + 100) {
                // ** top **
                // Step 2: make sure the current y postoin isn't greater then the previous y postion
                if (location. y <= originalLocation.y) {
                   // NSLog(@"This is the offset y %f", self.personTableView.contentOffset.y);

                        // Step 4: check to see if we have a timer and if not create one
                        [self startTimer];


                }
                else{
                    //Step 3: they started to scroll up so end the timer
                    [self endTimer];
                }
            }
            else if(location.y > tableViewHight - 100)
            {
                // ** bottom **
                // Step 2: make sure the current y postoin isn't less then the previous y postion
                if (location. y >= originalLocation.y) {

                        NSLog(@"its less then 0");
                        if (!timer) {
                            // Step 4: check to see if we have a timer and if not create one
                            [self startTimer];
                        }


                }
                else{
                     //Step 3: they started to scroll up so end the timer
                    [self endTimer];
                }
            }
            else{
                // ** middle **
                // Step 2: check to see if we have a timer and if so destory it
                [self endTimer];
            }

Basically I want to trigger that if statement when the cell is less then 100px from the hight of the scrollable table view.

Was it helpful?

Solution

From the UILongPressGestureRecognizer you can get the current y position of the touch. From that you can calculate how close to the bottom or top of the UITableViewthe touch is. If you determine that it's close enough to begin scrolling, then start a repeating NSTimer that increments or decrements the contentOffset of your UITableView by 1 pixel, scrolling your table. Change how fast it scrolls by changing the timeInterval of the timer.

When the UILongPressGestureRecognizer ends, or if the y position of the touch moves outside of the scrolling zone, then invalidate your timer, which will stop the scrolling.

If you felt adventurous you could also increase the speed of the scrolling dynamically as the user gets closer to the edge of the UITableView by changing the timeInterval as the user moves their touch up or down, as that is generally how this type of functionality works.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top