Change NSTimer interval after a certain number of fires

£可爱£侵袭症+ 提交于 2019-12-02 21:17:56

问题


In the following code, the NSTimer interval is set at 1 second between each picture. My goal is to change the interval after the first two pictures, hello.png and bye.png, to 4 seconds.

- (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];
}

回答1:


Make the timer object a member variable. Initially set animation time as 1 second. In the callback invalidate the timer and create a new one with 1 or 4 seconds depending on the counter.

@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];
}



回答2:


my goal is to change the interval

You cannot change a timer in any way. To change the firing interval, invalidate and destroy the timer and make a new timer with the new interval.

To do that, you will need to have kept a reference to the timer, as a property of your view controller (something that you have fatally failed to do in your implementation - you would have had no way to stop the timer, and you would have crashed if your view controller ever went out of existence, or else your view controller would have leaked because the timer retains it).




回答3:


The best/simpliest way :

    -(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 is a variable that contains the interval. You just need to call Timer method by yourself the first time ;)

For you, it would give

    -(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];
    }


来源:https://stackoverflow.com/questions/30633743/change-nstimer-interval-after-a-certain-number-of-fires

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!