在一定数量的火灾后更改NSTimer间隔

时间:2015-06-04 02:02:16

标签: objective-c cocoa-touch nstimer intervals

在以下代码中,每张图片之间的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];
}

3 个答案:

答案 0 :(得分:1)

  

我的目标是更改间隔

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

要执行那个,你需要保留对计时器的引用,作为你的视图控制器的一个属性(你实际上没有做到的事情 - 你本来应该做的)无法停止计时器,如果您的视图控制器已经不存在,您就会崩溃,否则您的视图控制器会因为计时器保留它而泄露。)

答案 1 :(得分:0)

使计时器对象成为成员变量。最初将动画时间设置为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];
}

答案 2 :(得分:-1)

最好/最简单的方式:

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