放大UIScrollView - 无法缩放超出单个图像并滚动

时间:2014-03-20 06:54:42

标签: ios objective-c uiscrollview uiimageview

我从NSURL加载一系列图像以滚动并捏缩放。我能够实现滚动部分,但是当我尝试将任何图像缩放到第一个图像之外时,滚动卡住了,图像上的比例也是如此。这是我的代码:

 scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, self.view.bounds.size.height)];
scrollView.showsHorizontalScrollIndicator = FALSE;
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.delegate = self;
[self.view addSubview:scrollView];


for (int i=0; i<[listOfImagesZoom count]; i++) {
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, self.scrollView.bounds.origin.y-100, SCREEN_WIDTH, self.view.bounds.size.height-100)];
    // [imageView setImage:[UIImage imageNamed:@"img_def.png"]];
    NSString *urlImage1 = (NSString*)[listOfImagesZoom objectAtIndex:i];
    NSString *productImageURL1 = [urlImage1 stringByReplacingOccurrencesOfString:@"~" withString:@""];
    NSString *productImageURL2 = [productImageURL1 stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString* urlImage = [NSString stringWithFormat:@"http://sagana-ideas.azurewebsites.net%@", productImageURL2];
    NSURL *imgURL=[NSURL URLWithString:urlImage];

    [imageView setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"ex_product.png"]];

    imageView.contentMode = UIViewContentModeScaleAspectFit;


    [scrollView addSubview:imageView];
    [imageView setUserInteractionEnabled:NO];
    imageView.tag = i;

    leftMargin += SCREEN_WIDTH;


}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
scrollView.delegate = self;

我甚至实现了

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    {

        return imageView;
    }

如果只有一个图像要加载,它会起作用,但是当图像多于一个时会卡住。

2 个答案:

答案 0 :(得分:1)

您需要ScrollView才能显示图像列表。每个图像都显示一个scrollView(子类scrollview)来执行放大/缩小。 试着看看这个:MWPhotoBrowser

答案 1 :(得分:0)

您可以在scrollview中创建一个UIView,您可以在其中添加所有图像,然后从- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView方法返回该视图。