1

我正在尝试实现一个计时器,但我的访问权限很差……我在 dealloc 中也没有使用 release。

.h 

   NSTimer *_timerAnimacao;

@property (nonatomic, retain) NSTimer *timerAnimacao;

.m 
@synthesize timerAnimacao = _timerAnimacao;

这是我调用计时器的地方。

  if(acondition)
       _timerAnimacao = [NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(changeTimeFilter:) userInfo:nil repeats:YES];
     else 
            _timerAnimacao = [NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(changeTimeFilterHide:) userInfo:nil repeats:YES];

工作changeTimeFilter正常......但changeTimeFilterHide正在获得BAD Access

-(void)changeTimeFilter:(NSTimer *)theTimer{

    if (tAnimacaoFilter<5) {
        [_tbFilters setFrame:CGRectMake(_tbFilters.frame.origin.x, _tbFilters.frame.origin.y, _tbFilters.frame.size.width, 58*tAnimacaoFilter)];
    }
    if(tAnimacaoFilter < 10) {
        tAnimacaoFilter++;
        [_tbFeeds setFrame:CGRectMake(_tbFeeds.frame.origin.x, _tbFeeds.frame.origin.y + (4.4*tAnimacaoFilter), _tbFeeds.frame.size.width, _tbFeeds.frame.size.height - (4.4*tAnimacaoFilter))];
        [imgBordaTbDireita setFrame:CGRectMake(imgBordaTbDireita.frame.origin.x, _tbFeeds.frame.origin.y - 4, imgBordaTbDireita.frame.size.width, imgBordaTbDireita.frame.size.height)];
        [imgBordaTbEsquerda setFrame:CGRectMake(imgBordaTbEsquerda.frame.origin.x, _tbFeeds.frame.origin.y, imgBordaTbEsquerda.frame.size.width, imgBordaTbEsquerda.frame.size.height)];
    }
    else {
        if(_timerAnimacao)
           [_timerAnimacao invalidate];
        [btnFilter setEnabled:YES];
    }
}

-(void)changeTimeFilterHide:(NSTimer *)theTimer{
    if (tAnimacaoFilter<5) {
        [_tbFilters setFrame:CGRectMake(_tbFilters.frame.origin.x, _tbFilters.frame.origin.y, _tbFilters.frame.size.width, (58*5) - (58*tAnimacaoFilter))];
    }
    if(tAnimacaoFilter < 10) {
        tAnimacaoFilter++;
        [_tbFeeds setFrame:CGRectMake(_tbFeeds.frame.origin.x, _tbFeeds.frame.origin.y - (4.4*tAnimacaoFilter), _tbFeeds.frame.size.width, _tbFeeds.frame.size.height + (4.4*tAnimacaoFilter))];
        [imgBordaTbDireita setFrame:CGRectMake(imgBordaTbDireita.frame.origin.x, _tbFeeds.frame.origin.y - 4, imgBordaTbDireita.frame.size.width, imgBordaTbDireita.frame.size.height)];
        [imgBordaTbEsquerda setFrame:CGRectMake(imgBordaTbEsquerda.frame.origin.x, _tbFeeds.frame.origin.y, imgBordaTbEsquerda.frame.size.width, imgBordaTbEsquerda.frame.size.height)];
    }

    else {
        if(_timerAnimacao)
            [_timerAnimacao invalidate];
        [btnFilter setEnabled:YES];
    }
}
4

2 回答 2

2

你需要做的无效是将 NSTimer 设置为 nil ,如下所示:

[_timerAnimacao invalidate];
_timerAnimacao = nil;

如果你不这样做,你的下一个命令将无法正常工作,例如:

if(_timerAnimacao)
  [_timerAnimacao invalidate];

请注意:if(_timerAnimacao)是一样的if(_timerAnimacao == nil)

也看看这个:如何使用 NSTimer?

于 2013-08-05T13:17:46.427 回答
0

NSTimer 的实例在您启动和无效时需要在内存中。

您可以通过两种方式做到这一点:

  1. 将 NSTimer 设置为 Appdelegate 类的属性,并在需要的任何地方使用它。

    NSTimer *timer=[(MyAppDelegate *)[[UIApplication sharedApplication] delegate].timer;

  2. 创建一个新类并将 NStimer 设置为属性。创建一个类方法以返回该类的单例实例。

    静态 MyClass *obj = NULL;

    • (MyClass *)instance { @synchronized(self) { if (obj == NULL) obj = [[self alloc] init]; }

      返回(对象);}

我更喜欢第二种方法。

于 2013-08-05T13:58:33.193 回答