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.
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.
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?