1

I have the following code to create a scroll view with additional area at the beginning and the end of the images (50 points).

UIScrollView* scroll = [[UIScrollView alloc] initWithFrame: CGRectMake(0,0,200,100)];
scroll.contentSize = CGSizeMake(400,100);

UIImageView* img1 = [[UIImageView alloc] initWithFrame: CGRectMake(50,0,100,100);
UIImageView* img2 = [[UIImageView alloc] initWithFrame: CGRectMake(150,0,100,100);
UIImageView* img3 = [[UIImageView alloc] initWithFrame: CGRectMake(250,0,100,100);

//Adding images to ImageViews

scroll.pagingEnabled = YES;
[scroll addSubView:img1];
[scroll addSubView:img2];
[scroll addSubView:img3];

The first time I see the view, I will see the additional area on the left (0-50), then the first image (50-150) and then half of the second image (150-200). When I swipe left, I want to see half of the first image on the right, the second image at the center, and half of the third image on the right.

When I swipe left again, I want to see the third image at center, with half of the second image on the left, and the additional area on the right.

Can it be possible?

Oded
  • 795
  • 2
  • 12
  • 32

3 Answers3

0

It's possible as long as you define everything correctly. Make sure that the assigned content size is equal to the overall size that you wish to scroll through. Make sure that each page size is equal to the scroll view's frame. You can set the clipsToBounds to NO if the frame clips your subviews.

Stavash
  • 14,244
  • 5
  • 52
  • 80
  • What do I need to define? I basically want each page to be 100 points, instead of 200 (content width / frame width). – Oded May 05 '13 at 09:05
  • This should help http://stackoverflow.com/questions/1677085/paging-uiscrollview-in-increments-smaller-than-frame-size – Stavash May 05 '13 at 09:46
  • And this http://stackoverflow.com/questions/1220354/uiscrollview-horizontal-paging-like-mobile-safari-tabs – Stavash May 05 '13 at 10:23
0

You can do this by adjusting the contentSize of UIScrollView

- (void)addImagesToScrollView{
    //An offset from where imageViewStarts
    CGFloat xOffset = 50.0f;
    CGRect imageViewFrame = CGRectMake(xOffset, 0.0f, 100.0f, 100.0f);

    for (NSString *imageName in @[@"image1.jpg",@"image2.jpg",@"image3.jpg"])
    {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:imageViewFrame];
        UIImage *image = [UIImage imageNamed:imageName];
        imageView.image = image;

        [self.scrollView addSubview:imageView];

        imageViewFrame.origin.x+=imageViewFrame.size.width;
    }

    CGSize contentSize = self.scrollView.frame.size;
    //Content size is calculate from the imageViewFrame and an offset is added to it
    contentSize.width = imageViewFrame.origin.x+xOffset;
    [self.scrollView setContentSize:contentSize];

}

Source Code

Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

You can scroll to a particular place in the scrollview using the following code

[YOURSCROLLVIEW setContentOffset:CGPointMake(x, y) animated:YES];
Manju
  • 4,133
  • 2
  • 19
  • 12