user3007919
user3007919

Reputation:

A UIScrollview that auto scroll and Stop at every Pages for atleast 2seconds

I am using NSTimer to move through my UIScrollview, but I can't get to make it stop at every page for like let's say a second or two so users can be able to preview and click if they want to or when they are scrolling around the images in the UIScrollview my Code is below....:

    -(void) onTimer {

CGPoint rightOffset = CGPointMake(responseScroll.contentSize.width - responseScroll.bounds.size.width, 0);
[responseScroll setContentOffset:rightOffset animated:YES];
}


 -(void)viewDidLoad {

 /-------/
 [NSTimer scheduledTimerWithTimeInterval:0.008 target:self selector:@selector(onTimer)   userInfo:nil repeats:YES]; 
 }

Upvotes: 0

Views: 1922

Answers (3)

Rahul K Rajan
Rahul K Rajan

Reputation: 796

Here is my answer, this is in swift. This will scroll the pages in scrollview infinitely.

private func startBannerSlideShow()
{
    UIView.animate(withDuration: 6, delay: 0.1, options: .allowUserInteraction, animations: {
        scrollviewOutlt.contentOffset.x = (scrollviewOutlt.contentOffset.x == scrollviewOutlt.bounds.width*2) ? 0 : scrollviewOutlt.contentOffset.x+scrollviewOutlt.bounds.width
    }, completion: { (status) in
        self.startBannerSlideShow()
    })
}

Upvotes: 0

Snowman
Snowman

Reputation: 32061

Make the timer interval 2.0:

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

This means that the timer will fire every 2 seconds.

If you want to shift one page over, you need to adjust your onTimer method:

CGPoint rightOffset = CGPointMake(responseScroll.contentOffset.x + responseScroll.frame.size.width, 0);
[responseScroll setContentOffset:rightOffset animated:YES];

Upvotes: 2

streem
streem

Reputation: 9144

Consider using UIAnimations instead

-(void)viewDidLoad{
    [self moveScrollView];
}

-(void)moveScrollView{
    CGPoint rightOffset = CGPointMake(responseScroll.contentOffset.x+responseScroll.bounds.size.width, 0);
    if(rightOffset.x+responseScroll.bounds.size.width<=responseScroll.contentSize.width){
        [UIView animateWithDuration:1 animations:^{
           [responseScroll setContentOffset:rightOffset animated:NO];
        } completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                [self moveScrollView];
            });
        }];
    }
}

Upvotes: 0

Related Questions