0

我正在添加AsyncImageViewUIScrollView从网络加载图像,它工作正常,我添加了 8 张图像。问题是paging我已经启用pagingEnableYES但它不会按照我想要的那样来,我正在尝试contentOffset用不同的方式设置它,x但它不会产生任何影响。我希望当我滚动或选择或选项时,每个图像都应该出现centerUIScrollViewnextprevious

这是我要添加图像的代码UIScrollView

arrImgUrls 是一个 NSArray

_currentImage, _imageCount 是整数

-(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);
}

这是我用来显示的代码previous或.nextUIScrollView

-(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!

4

2 回答 2

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

于 2012-10-04T10:36:19.693 回答
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.

于 2012-10-04T11:29:36.773 回答