0

我已经设置了一些代码来在 TimeInterval 之后自动滚动(分页)滚动视图。现在我想在 4 次后停止滚动视图动画。有人可以教我怎么做吗?

这是我的代码。

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

- (void) onTimer {

    // Updates the variable h, adding 320
    abc += 320;

    //This makes the scrollView scroll to the desired position
    [UIView animateWithDuration:1.5f animations:^{
      [scrollView setContentOffset:CGPointMake(abc, 0) animated:NO];
        }];
}
4

1 回答 1

2

首先将 ivar 添加到 NSTimer

{
    NSTimer *timer;
}

timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

然后在onTimer

- (void) onTimer {
    //Create a static int
    static int repetition = 0;
    repetition ++;
    if(repetition == 4)
    {
       repetition = 0;
       //Stop the timer
       [timer invalidate];
    }

    // Updates the variable h, adding 320
    abc += 320;

    //This makes the scrollView scroll to the desired position
    [UIView animateWithDuration:1.5f animations:^{
        [scrollView setContentOffset:CGPointMake(abc, 0)animated:NO];


    }];
}
于 2012-11-07T18:56:01.360 回答