iOS : UIScrollView paging not working properly

蹲街弑〆低调 提交于 2019-12-12 05:27:29

问题


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!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12722254/ios-uiscrollview-paging-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!