0

I have small problem with NSTimer in long press gesture. How to invalidate timer for any press? If I've long pressed the timer is start count off from any pressed cells. For example I long pressed 3 cells the timer working 3 times.

I have no Idea. Here is my code below where I tried after some times to give a condition and now hideButtonTimer does not count.

I have NSTimer declares as:

NSTimer *hideButtonTimer;

My setup gesture recognizer:

- (void)setupGestureRecognizersForTableView {
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[lpgr setMinimumPressDuration:1.0];
[lpgr setDelegate:self];
[self.tableView addGestureRecognizer:lpgr]; }

and function handleLongPress

- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    
    
    CGPoint p = [gestureRecognizer locationInView:self.tableView];
    selectedIndexPath = [self.tableView indexPathForRowAtPoint:p];
    

    if (selectedIndexPath != nil && gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [self.tableView reloadData];
        [self resetTableView];
        
        hideButtonTimer = nil;

        PresentationCell *cell = (PresentationCell *)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
        NSLog(@"start click");
        [cell.deleteButton setHidden:NO];
        [cell.updateShowView setHidden:YES];
        if (IS_IPAD) {
                [cell.nameLabel setHidden:NO];
                [cell.descriptionLabel setHidden:NO];
        } else {
                [cell.nameLabel setHidden:YES];
                [cell.descriptionLabel setHidden:YES];
        };
        
        if (hideButtonTimer == nil) {
            hideButtonTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(hideButton) userInfo:nil repeats:NO];
        }
        
        [hideButtonTimer invalidate];
        hideButtonTimer = nil;
        
        //NSLog(@"%@", hideButtonTimer);
    }
}

and next last function hideButton:

- (void)hideButton {
    NSLog(@"hide!");
    PresentationCell *cell = (PresentationCell *)[_tableView cellForRowAtIndexPath:selectedIndexPath];
    cell.deleteButton.hidden = YES;
    cell.nameLabel.hidden = NO;
    cell.descriptionLabel.hidden = NO;
//    [hideButtonTimer invalidate];
//    hideButtonTimer = nil;
}

Please help. I will be very grateful.

handzel
  • 91
  • 1
  • 10
  • 1
    Unclear what the actual problem is. – matt Aug 20 '20 at 15:51
  • @matt I need reset timer for any tap. If I tap only first cell and wait 5 seconds its okay, but If I tap first and second cell after 2 seconds the cell will hide after 3 seconds, not 5 seconds... – handzel Aug 20 '20 at 18:54

1 Answers1

1

EDIT

Ok I've changed it a bit based on what I understand you want. Timer resets whenever there is a tap, but still it only fires once. Hope this is what you want.

Try the following.

  1. Add ivar
@property (nonatomic) BOOL pressed;
  1. Change hideButton (remove those commented out stuff)
- (void)hideButton {
    NSLog(@"hide!");
    PresentationCell *cell = (PresentationCell *)[_tableView cellForRowAtIndexPath:selectedIndexPath];
    cell.deleteButton.hidden = YES;
    cell.nameLabel.hidden = NO;
    cell.descriptionLabel.hidden = NO;
    [hideButtonTimer invalidate];
    hideButtonTimer = nil;
}
  1. Long press, reset timer when a tap is seen while timer is counting down
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    
    
    CGPoint p = [gestureRecognizer locationInView:self.tableView];
    selectedIndexPath = [self.tableView indexPathForRowAtPoint:p];
    

    if (selectedIndexPath != nil && gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [self.tableView reloadData];
        [self resetTableView];
        
        PresentationCell *cell = (PresentationCell *)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
        NSLog(@"start click");
        [cell.deleteButton setHidden:NO];
        [cell.updateShowView setHidden:YES];
        if (IS_IPAD) {
                [cell.nameLabel setHidden:NO];
                [cell.descriptionLabel setHidden:NO];
        } else {
                [cell.nameLabel setHidden:YES];
                [cell.descriptionLabel setHidden:YES];
        };
        
        // Use ivar
        if ( ! self.pressed ) {
            self.pressed = YES; // Never fire again
            hideButtonTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(hideButton) userInfo:nil repeats:NO];
        }
        // Reset timer if it is counting down
        else if ( hideButtonTimer ) {
           // Invalidate old timer
           [hideButtonTimer invalidate];
           // Start a new one
           hideButtonTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(hideButton) userInfo:nil repeats:NO];
        }
        // else timer already fired - no action
        
        //NSLog(@"%@", hideButtonTimer);
    }
}

So you use an ivar to remember the press, not the timer itself.

PS : this may not be exactly what you want, but use an ivar together with a timer to get what you want.

PSS : The timer - not clear what it is from the code, but probably should be an ivar as well?

skaak
  • 2,988
  • 1
  • 8
  • 16
  • Thank you for answer! Actually isn't working look I want because I need clean/reset timer for any tap, because If I tap first cell and second or third cell after two seconds this cell will hidden after 3 second not 5 seconds, why? – handzel Aug 20 '20 at 18:47
  • Let me clarify ... on any tap you start a timer. 1) Then when another tap happens you want to reset the timer? Ok that can be done easily. 2) The timer fires only once and hide or show something correct? Or does the timer fire many times? – skaak Aug 20 '20 at 19:15
  • See edits, the title should perhaps be 'How to reset/restart timer on tap' – skaak Aug 20 '20 at 20:04