我嘗試使用nstimer點擊顯示/隱藏視圖。如何取消之前的NSTimer如果新的被觸發
@interface
@property (nonatomic, retain)NSTimer *timer;
UIView *view;
@implementation
view.hiden=YES;
- (void)handleTap:(UITapGestureRecognizer *)sender{
if([timer isValid)]{
timer.invalidate;///that gives me _EXC_BAD_ACCESS
}
view.hiden=NO;
timer = [NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:@selector(targetMethod)
userInfo:nil
repeats:NO];
}
-(void)targetMethod{
view.hiden=YES;
}
如果您瞭解此代碼的某些內容,則可以看到該問題。如果我點擊一次並等待3秒鐘,則效果很好。但如果我點擊它並在1.5秒後再次點擊,它應該製作另一個定時器盒並在3秒後隱藏它,但它在1.5秒後隱藏它,因爲舊的正在射擊。你可以看到,我試圖使它無效,但給我錯誤。
那麼如何以適當的方式摧毀它?
謝謝,這是有益的;) –
我又增加了子彈點到列表中。 –