Search code examples
xcodebuttonscrollplaybackautoscroll

xcode how can i make a scrollView scroll very slowly to the end without user interaction?


i have a scrollView and i need it to scroll very slowly down to the end, by itself, and when the user touches the view it should respond to the gestures(up down). together with this i need to put a button that stops/plays the autoscroll.

the code:

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

UIButton *camilaButton = [UIButton buttonWithType:UIButtonTypeCustom];
[camilaButton setImage:[UIImage imageNamed:@"camila.jpg"] forState:UIControlStateNormal];
[camilaButton setImage:[UIImage imageNamed:@"camila.jpg"] forState:UIControlStateHighlighted];
camilaButton.frame = CGRectMake(20, 10, 280, 200);

[self.scrollView addSubview:camilaButton];
[camilaButton addTarget:self action:@selector(onButtonPressCamila) forControlEvents:UIControlEventTouchUpInside];

UILabel *camilaLabel = [[UILabel alloc] initWithFrame:CGRectMake(40,220,100,30)];
camilaLabel.text = @"Camila";
[self.scrollView addSubview:camilaLabel];

... and then there are similar buttons with images below to the end of the scrollview.

how can i do this?

Mihai


Solution

  • this adapted code did the trick (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;
    }
    }