how can I make a scrollView autoscroll?

11,404

Solution 1

this adapted code did the trick (source http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/)

PS : each image is 280 by 200

  - (void)viewDidLoad
 {
[super viewDidLoad];

UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
 }

  - (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.

for (int i=1; i<=10; i++) {
    // create image
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.jpg",i]];
    // create imageView
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, ((i-1)*scrMain.frame.size.height+100), 280, 200)];
    // set scale to fill
    imgV.contentMode=UIViewContentModeScaleToFill;
    // set image
    [imgV setImage:image];
    // apply tag to access in future
    imgV.tag=i+1;
    // add to scrollView
    [scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width, scrMain.frame.size.height*10)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];


}

 - (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.y;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
// if page is not 10, display it
if( nextPage!=10 )  {


    [scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
    pgCtr.currentPage=nextPage;
    // else start sliding form 1 :)


} else {

    [scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
    pgCtr.currentPage=0;
}
}

Solution 2

You can use:

  [_scrollView setContentOffset:CGPointMake(x,y) animated:YES];

and use the x and y as the touch points on the screen you can capture.

You can also do an animation with CoreAnimation:

 [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
 [UIScrollView setAnimationDuration:1.0f];
 [scroll setContentOffset:CGPointMake(x, y)];
 [UIScrollView commitAnimations];

Solution 3

You can set x if you want to scroll horizontally, otherwise set y to scroll vertical.

[_scrollView setContentOffset:CGPointMake(x, y) animated:YES];   

and modify the co-ordinates accordingly.

Share:
11,404

Related videos on Youtube

Mihai
Author by

Mihai

Updated on July 28, 2022

Comments

  • Mihai
    Mihai over 1 year

    I have this scrollView:

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.scrollView.contentSize = CGSizeMake(320,3000);
    
    
    _scrollView.frame = CGRectMake(0, 45, 320, 420);
    

    and I want to make it autoscroll very slowly downward to the end so that the user can see the content (as in movie credits), eventually with a button to stop/play, but to follow the user gestures when touching the interface.

    How can I do this? Thanks,

  • Mihai
    Mihai over 10 years
    sorry i'm a newby, what is x, y supposed to be changed with?
  • apollosoftware.org
    apollosoftware.org over 10 years
    the coordinates on the screen x is the horizontal location of touch, y is the vertical.
  • apollosoftware.org
    apollosoftware.org over 10 years
    values that are valid are 0..Screen Width/Height max. For example an iPad without retina is 768x1024 for full screen in portrait orientation. Tinker with the values and you'll see.
  • Mihai
    Mihai over 10 years
    ah, sorry, my bad, now i understand, what i meant was that i want it to scroll by itself very slowly as in movie final credits
  • apollosoftware.org
    apollosoftware.org over 10 years
    set the duration to like 5.0f i think that's 5 seconds. or whatever number you wish.
  • Mihai
    Mihai over 10 years
    [UIScrollView setAnimationDuration:5.0f]; [_scrollView setContentOffset:CGPointMake(0, 2500) animated:YES]; [UIScrollView commitAnimations]; it gets to the location ,but not with delay, when i open it it is at hte location already
  • Mihai
    Mihai over 10 years
    thanks, how can i input a delay so that the user can see the content up to that location?
  • apollosoftware.org
    apollosoftware.org over 10 years
    remove the animated:YES because you are alrady using CoreAnimation to do it, you need to override. animated:YES will use the default sans the delay