179

I have a UIScrollView which has several views. When a user flicks their finger, the view scrolls to the right or left depending on the direction of the finger flick. Basically my code works in a way similar to the iPhone photo app. Now, is there a way that I can programmatically do the same thing so that I end up with a slideshow that runs on its own with a click of a button and a configurable pause between each scroll?

How do you really do slideshows with UIScrollView?

Nazik
  • 8,696
  • 27
  • 77
  • 123
climbon
  • 2,273
  • 4
  • 17
  • 17

10 Answers10

425

You can scroll to some point in a scroll view with one of the following statements in Objective-C

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

or Swift

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

See the guide "Scrolling the Scroll View Content" from Apple as well.

To do slideshows with UIScrollView, you arrange all images in the scroll view, set up a repeated timer, then -setContentOffset:animated: when the timer fires.

But a more efficient approach is to use 2 image views and swap them using transitions or simply switching places when the timer fires. See iPhone Image slideshow for details.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    Cool. Yes, I did find that setContentOffset works but really wanted this to happen in animated way. 'animated:YES' did the trick. – climbon Feb 12 '10 at 04:11
  • 3
    Just complementing the answer, to move horizontally to the next "page" in UIScrollView (assuming you are coding for iPhone), for the x parameter use `(myScrollView.contentOffset.x +320)`. – Giovanni Sep 25 '12 at 00:05
  • 2
    @niraj thanks dude...in `(myScrollView.contentOffset.x +320)` lies the key! – DD_ Feb 28 '13 at 06:17
  • 5
    Correct me if I'm wrong, but UIScrollView's `contentOffset:` will offset the contentSize as well, which may not be desirable. To only scroll, you may want to use `scrollRectToVisible:`. – Chris Oct 29 '13 at 20:27
  • Do no use C helper functions like `CGPointMake` in Swift. Simply build a Swift struct directly: `CGPoint(x: 0, y: 44)` – Arnold Jul 11 '15 at 10:13
  • [scrollView scrollRectToVisible:CGRectMake(x, y, width, height), animated:YES]; when i'm adding this it is slightly scrolled upwards initially. Can you give me a fix @kennytm ? – Abhishek Thapliyal Jul 02 '16 at 09:00
42

If you want control over the duration and style of the animation, you can do:

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
    scrollView.contentOffset = CGPointMake(x, y);
} completion:NULL];

Adjust the duration (2.0f) and options (UIViewAnimationOptionCurveLinear) to taste!

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
20

I'm amazed that this topic is 9 years old and the actual straightforward answer is not here!

What you're looking for is scrollRectToVisible(_:animated:).

Example:

extension SignUpView: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        scrollView.scrollRectToVisible(textField.frame, animated: true)
    }
}

What it does is exactly what you need, and it's far better than hacky contentOffset

This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing.

From: https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

vol
  • 1,594
  • 11
  • 16
12

Another way is

scrollView.contentOffset = CGPointMake(x,y);
Luke
  • 11,426
  • 43
  • 60
  • 69
11

With Animation in Swift

scrollView.setContentOffset(CGPointMake(x, y), animated: true)
Michael
  • 9,639
  • 3
  • 64
  • 69
6

Swift 3

let point = CGPoint(x: 0, y: 200) // 200 or any value you like.
scrollView.contentOffset = point
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69
3
scrollView.setContentOffset(CGPoint(x: y, y: x), animated: true)
CoderPug
  • 908
  • 9
  • 19
2
[Scrollview setContentOffset:CGPointMake(x, y) animated:YES];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
0
- (void)viewDidLoad
{
    [super viewDidLoad];
    board=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 80)];
    board.backgroundColor=[UIColor greenColor];
    [self.view addSubview:board];
    // Do any additional setup after loading the view.
}


-(void)viewDidLayoutSubviews
{


    NSString *str=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    index=1;
    for (int i=0; i<20; i++)
    {
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(-50, 15, 50, 50)];
        lbl.tag=i+1;
        lbl.text=[NSString stringWithFormat:@"%c",[str characterAtIndex:arc4random()%str.length]];
        lbl.textColor=[UIColor darkGrayColor];
        lbl.textAlignment=NSTextAlignmentCenter;
        lbl.font=[UIFont systemFontOfSize:40];
        lbl.layer.borderWidth=1;
        lbl.layer.borderColor=[UIColor blackColor].CGColor;
        [board addSubview:lbl];
    }

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

    NSLog(@"%d",[board subviews].count);
}


-(void)CallAnimation
{

    if (index>20) {
        index=1;
    }
    UIView *aView=[board viewWithTag:index];
    [self doAnimation:aView];
    index++;
    NSLog(@"%d",index);
}

-(void)doAnimation:(UIView*)aView
{
    [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
        aView.frame=CGRectMake(self.view.frame.size.height, 15, 50, 50);
    }
                     completion:^(BOOL isDone)
     {
         if (isDone) {
             //do Somthing
                        aView.frame=CGRectMake(-50, 15, 50, 50);
         }
     }];
}
user1781290
  • 2,674
  • 22
  • 26
Naveen
  • 1
  • 1
-1

Here is another use case which worked well for me.

  1. User tap a button/cell.
  2. Scroll to a position just enough to make a target view visible.

Code: Swift 5.3

// Assuming you have a view named "targeView"
scrollView.scroll(to: CGPoint(x:targeView.frame.minX, y:targeView.frame.minY), animated: true)

As you can guess if you want to scroll to make a bottom part of your target view visible then use maxX and minY.

Mussa Charles
  • 4,014
  • 2
  • 29
  • 24