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.