我在长按手势时对NSTimer有小的问题。如何使任何计时器的计时器无效?如果我长按了此计时器,则从任何按下的单元格开始计时。例如,我长按3个单元,使计时器工作3次。
我不知道。这是下面的代码,经过一段时间后我尝试给出条件,但现在hideButtonTimer
不算在内。
我有NSTimer声明为:
NSTimer *hideButtonTimer;
我的设置手势识别器:
- (void)setupGestureRecognizersForTableView {
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[lpgr setMinimumPressDuration:1.0];
[lpgr setDelegate:self];
[self.tableView addGestureRecognizer:lpgr]; }
和功能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);
}
}
和倒数第二个函数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;
}
请帮助。我将非常感谢。