我是想达到这种效果:
点击屏幕后显示按钮(原先为隐藏),同时启动定时器。
若接下来没有再点击屏幕,则3秒后隐藏按钮;若在控件消失前(3秒内)再点击屏幕,那么计时器就重新计时.。。可是现在出现了这样的情况,连续点击屏幕,3秒后,在连续点击序幕的过程中,按钮会不间断消失,出现,消失,出现。我想是因为之前的定时器让它消失的原因这是我的代码:请各位指教,谢谢!-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesEnded");
    buttom.hidden=NO;    timer = [NSTimer scheduledTimerWithTimeInterval: 3.0f
             
                                             target: self
             
                                           selector: @selector(handleTimer:)
         
                                           userInfo: nil
             
                                            repeats: NO];
    
    
}- (void) handleTimer: (NSTimer *) timer{
    
    //在这里进行处理
    buttom.hidden=YES;
    [timer invalidate];
    timer = nil;
       
}

解决方案 »

  1.   

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"touchesEnded");
        btn.hidden=NO;
            [timer invalidate];
        timer = nil;    timer = [NSTimer scheduledTimerWithTimeInterval: 3.0f
                 
                                                 target: self
                 
                                               selector: @selector(handleTimer:)
                 
                                               userInfo: nil
                 
                                                repeats: YES];
        
        
    }- (void) handleTimer: (NSTimer *) timer
    {
        NSLog(@"handleTimer");
        //在这里进行处理
        btn.hidden=YES;    
    }
      

  2.   

    不行的,这么修改后出现这样的情况:
    连续点击屏幕,按钮就一直存在,之前那种不停消失又出现的情况没有了,但是等到按钮消失后再点击屏幕,程序就会中断报错:Thread1:exc_bad_access
      

  3.   

    或者://@property (nonatomic, retain) NSTimer *timer;-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"touchesEnded");
        btn.hidden=NO;
        
        [timer invalidate];
        self.timer = nil;
            self.timer = [NSTimer scheduledTimerWithTimeInterval: 3.0f
                 
                                                 target: self
                 
                                               selector: @selector(handleTimer:)
                 
                                               userInfo: nil
                 
                                                repeats: NO];
        
        
    }- (void) handleTimer: (NSTimer *) timer
    {
        NSLog(@"handleTimer");
        //在这里进行处理
        btn.hidden=YES;
    }
      

  4.   

    我找到问题所在了,是因为timer命名相同的原因,谢谢你啊!