简体   繁体   中英

Stop animation on UIScrollview

I've set up some code to scroll (paging) the scrollview automatically, after a TimeInterval. Now I want to stop the scrollview animating, after 4 times. Could someone teach me how to do that?

This is my code.

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

First add an ivar to an NSTimer

{
    NSTimer *timer;
}

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

Then in the 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];


    }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM