[英]How to reset/restart NSTimer in tap LongPressGesture Objective-C
我在長按手勢中遇到 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]; }
和 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);
}
}
接下來是 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;
}
請幫忙。 我將不勝感激。
編輯
好的,我根據我對您的需求進行了一些更改。 只要有水龍頭,定時器就會重置,但它仍然只觸發一次。 希望這就是你想要的。
嘗試以下操作。
@property (nonatomic) BOOL pressed;
- (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;
}
- (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);
}
}
因此,您使用 ivar 來記住新聞,而不是計時器本身。
PS:這可能不是你想要的,但是使用一個ivar和一個計時器來得到你想要的。
PSS:計時器 - 從代碼中不清楚它是什么,但可能也應該是一個 ivar?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.