0

Есть 10 страниц которые нужно переворачивать через разные промежутки времени. Нужно создать 10 таймеров для переворота или можно это сделать проще? Как сделать проще?

3
  • Массив с интервалами и реинициализация таймера по списку?
    – iosp
    17 авг 2016 в 17:55
  • создать таймер, и когда он сработает создать следующий и так далее 17 авг 2016 в 18:24
  • @iosp могли бы вы привести пример в коде?
    – user214155
    17 авг 2016 в 19:20

1 ответ 1

1

Пример ViewController:

@interface ViewController () {
    NSTimer *timer;
    NSArray *intervals;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    intervals = @[@1.0f,@2.0f,@3.0f,@2.0f,@1.0f,@1.0f,@2.0f,@3.0f,@2.0f,@1.0f];
    [self setTimerPeriod:0];
}

-(void) cleanTimer {
    if (timer != nil) {
        [timer invalidate];
        timer = nil;
    }
}

-(void) setTimerPeriod: (NSInteger) index {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (timer != nil) {
            [timer invalidate];
            timer = nil;
        }
        if (timer == nil) {
            timer = [NSTimer scheduledTimerWithTimeInterval:[intervals[index] floatValue] target:self selector:@selector(timerUpdate:) userInfo:@(index) repeats:NO];
        }
    });
}

-(void) timerUpdate:(NSTimer*)sender {
    NSInteger index = [sender.userInfo integerValue];
    index++;
    if (index<intervals.count) {
        [self setTimerPeriod:index];
        NSLog(@"Fired. New index is: %@",@(index));
    } else {
         NSLog(@"Fired. Stop.");
        [self cleanTimer];
    }
}
2
  • А действие переворота нужно указывать в timerUpdate?
    – user214155
    18 авг 2016 в 7:11
  • Да. Туда попадает при каждом срабатывании таймера
    – iosp
    18 авг 2016 в 8:52

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.