Search code examples
iosuiscrollview

How can I programmatically scroll UIScrollView from start to end slowly


I've a horizontal UIScrollview that shows around 10 images. I know that we've to use scrollRectToVisible method to move the scrollview programmatically. But what I am looking for is to scroll the scroll view slowly (5 pixels in 1 second) from start to end of the scroll view.

I have seen some pages, but I didn't understand how to integrate following code in my code:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:(abs(rMid-pMid)*0.3)];
scrollMid.contentOffset = CGPointMake(rMid*320, 0);
[UIView commitAnimations];

Solution

  • you can use NSTimer like

    - (void) viewDidLoad
    {       
        if (scrollingTimer == nil)
        {
            scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
                             target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];        
        }
    }
    
    - (void) autoscrollTimerFired
    {
        if (scrollPoint.y == 583) // at where you want to stop scroll
        {
            [scrollingTimer invalidate];
            scrollingTimer = nil;
        }
        scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);
        [self.scrollView setContentOffset:scrollPoint animated:NO];
    }
    

    hope its help you...