【问题标题】:Change NSTimer interval after a certain number of fires在一定数量的火灾后更改 NSTimer 间隔
【发布时间】:2015-08-18 11:14:57
【问题描述】:

在以下代码中,NSTimer 间隔设置为每张图片之间的 1 秒。我的目标是将前两张图片 hello.png 和 bye.png 之后的间隔更改为 4 秒。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png",@"bye again"];

    self.images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
    self.animationImageView.image = self.images[0];
    [self.view addSubview:self.animationImageView];
    self.animationImageView.userInteractionEnabled = TRUE;



    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
}


-(void)changeImage:(NSTimer *) timer {
    if (counter == self.images.count - 1 ) {
        counter = 0;
    }else {
        counter ++;
    }
    self.animationImageView.image = self.images[counter];
}

【问题讨论】:

    标签: objective-c cocoa-touch nstimer intervals


    【解决方案1】:

    我的目标是改变间隔

    您不能以任何方式更改计时器。要更改触发间隔,请使计时器无效并销毁,并使用新的间隔创建一个新计时器。

    要做到,您需要保留对计时器的引用,作为视图控制器的一个属性(您在实现中完全没有做到这一点 - 您本来可以没有办法停止计时器,如果您的视图控制器不存在,您就会崩溃,否则您的视图控制器会因为计时器保留它而泄漏)。

    【讨论】:

    • 关于定时器管理,见我的书:apeth.com/iOSBook/…(向下滚动到关于通知的部分到关于定时器的部分)
    【解决方案2】:

    使定时器对象成为成员变量。最初将动画时间设置为 1 秒。在回调中使计时器无效并根据计数器创建一个 1 或 4 秒的新计时器。

    @interface ViewController ()
    @property (strong,nonatomic) NSMutableArray *images;
    @property (strong,nonatomic) UIImageView *animationImageView;
    {
        NSTimer *_timer;
    }
    @end
    
    @implementation ViewController {
        NSInteger counter;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png",@"bye again"];
    
        self.images = [[NSMutableArray alloc] init];
        for (int i = 0; i < imageNames.count; i++) {
            [self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
        }
    
        self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
        self.animationImageView.image = self.images[0];
        [self.view addSubview:self.animationImageView];
        self.animationImageView.userInteractionEnabled = TRUE;
    
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
        singleTap.numberOfTapsRequired = 1;
        singleTap.numberOfTouchesRequired = 1;
        [self.animationImageView addGestureRecognizer:singleTap];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
    }
    
    
    -(void)changeImage:(NSTimer *) timer {
         [_timer invalidate];
        if (counter == self.images.count - 1 ) {
            counter = 0;
        }else {
            counter ++;
        }
        if(counter == 0 || counter == 1)
        {
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
        }
        else if(counter == 2 || counter == 3)
        {
            _timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
        }
        self.animationImageView.image = self.images[counter];
    }
    

    【讨论】:

    • @katie 你是否在 changeImage 方法中使计时器无效?
    • 对不起,我认为这是我自己的错误。它工作得很好。谢谢。我正在拍照片。
    【解决方案3】:

    最好/最简单的方法:

        -(void)Timer{
    
        // Do what you want here
    
      // modify the frequency value if you want.  
            [NSTimer scheduledTimerWithTimeInterval:frequence target:self selector:@selector(Timer) userInfo:nil repeats:NO];
        }
    

    Frequence 是一个包含区间的变量。 您只需要第一次自己调用 Timer 方法;)

    对你来说,它会给

        -(void)changeImage{
            if (counter == self.images.count - 1 )
                counter = 0;
            else
                counter ++;
    
            frequence = (counter < 2)?1:4;
    
            self.animationImageView.image = self.images[counter];
            [NSTimer scheduledTimerWithTimeInterval:frequence target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多