0

I'm adding AsyncImageView into UIScrollView to load images from web, its working fine, there are 8 images i've added. Problem is of paging I've enabled pagingEnable to YES but it won't come as per I want, I'm trying to setting it contentOffset with different x but it won't any effects. I want that each image should come in center of UIScrollView when I scroll or choose next or previous option.

This is the code where I'm adding images to UIScrollView

arrImgUrls is an NSArray

_currentImage, _imageCount are integers

-(void)setImagesToScrollView
{
    [btnPrev setEnabled:NO];

    _currentImage = 0;

    _imageCount = [arrImgUrls count];

    int x=20;
    int y=0;
    int w=213;
    int h=160;

    for (int i=0; i<[arrImgUrls count]; i++) 
    {
        AsyncImageView *asyncImageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        [asyncImageView setDelegate:self];
        [asyncImageView.layer setMasksToBounds:YES];        
        NSString *urlImage = [arrImgUrls objectAtIndex:i];
        NSURL *url = [NSURL URLWithString:urlImage];
        [asyncImageView loadImageFromURL:url];
        [asyncImageView release];
        [scrollImages addSubview:asyncImageView];
        x=(x+w)+10;
    }

    scrollImages.contentSize=CGSizeMake(x, scrollImages.frame.size.height);
}

This is code I used to show previous or next images in UIScrollView.

-(IBAction)prevImage:(id)sender
{    
    _currentImage--;

    [btnNext setEnabled:YES];
    
    if (_currentImage<=0)
    {
        _currentImage = 0;
        [btnPrev setEnabled:NO];
    }

    [scrollImages setContentOffset:CGPointMake((_currentImage*imageWidth)+10, 0) animated:NO];
}

-(IBAction)nextImage:(id)sender
{    
    _currentImage++;

    [btnPrev setEnabled:YES];

    if (_imageCount-1 == _currentImage)
    {
        [btnNext setEnabled:NO];
    }

    [scrollImages setContentOffset:CGPointMake(_currentImage*imageWidth, 0) animated:NO];
}

All images loaded and added to UIScrollView only problem is to show it in center when user scrolls or do previous or next.

Please point me where I'm doing wrong?

Thanks!

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186

2 Answers2

3

First of all make sure that _currentImage is maintain properly or not?

Now try following for next image

CGRect frame;
frame.origin.x = (imageWidth * _currentImage) +10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES]; // NO if you don't want animation

Instead of this line

[scrollImages setContentOffset:CGPointMake(_currentImage*imageWidth+10, 0) animated:NO];

& for previous image

CGRect frame;
frame.origin.x = (imageWidth * _currentImage) - 10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES]; // NO if you don't want animation

and here one nice example just like you want

Image gallary

Hemang
  • 26,840
  • 19
  • 119
  • 186
P R J
  • 428
  • 5
  • 18
  • Not yet! :( but the example you've linked is good and same as I want, I'm taking help from it. – Hemang Oct 04 '12 at 12:18
  • @Hemang ok...try it & tell me if you solved it...good luck & one thing i have noticed in your question im nextimg you have written [scrollImages setContentOffset:CGPointMake((_currentImage*imageWidth)+10, 0) animated:NO]; here instead of +10 make -10 & innext image use +10 try it & tell me if it worked – P R J Oct 04 '12 at 12:28
  • PRJ, Solved it !! Spend a day in this :(. Your example makes it. Thanks a ton. – Hemang Oct 04 '12 at 13:11
0

The problem is with frame that you are setting for AsyncImageView. You must be familiar how the paging working. It checks for rect you have assigned to scrollview & it scroll to the center of the scrollview hence you might seeing the image are placed towards left or right side not aligned center. Try setting frame AsyncImageView as x= 0 & width = 320 & see the behavior of the scrollview.

Anand
  • 2,086
  • 2
  • 26
  • 44