仅从UIScrollView中缩放选定的子视图

时间:2013-07-23 11:13:23

标签: iphone uiscrollview zooming uiscrollviewdelegate subviews

我在UIScrollView中添加了许多UIImageView,并启用了分页。我只需要缩放我点击缩放的图像,而不是缩放整个滚动视图。同时缩放特定图像也不会缩放其他子视图。

- (void)loadScrollViewWithImages {

scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * imageList.count, self.view.bounds.size.height);
scrollView.pagingEnabled = YES;
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];

CGFloat xPos = 0.0;
for (UIView *subView in scrollView.subviews) {
    [subView removeFromSuperview];
}

int i = 0;

for (NSDictionary *imageDetails in self.imageList) {

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, 0.0, self.view.bounds.size.width, self.view.frame.size.height)];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [imageView setClipsToBounds:YES];
    [mainView addSubview:imageView];
    xPos += self.view.bounds.size.width;

    [imageView setImageWithURL:[NSURL URLWithString:[imageDetails objectForKey:@"media_path"]]];

    [self.imageViewArray addObject:imageView];

    if(self.selectedImageIndex == i) {
        self.selectedImageView = imageView;
    }
    i++;
}
[mainView setFrame:CGRectMake(mainView.frame.origin.x, mainView.frame.origin.x,self.view.bounds.size.width * imageList.count, self.view.bounds.size.height)];
[scrollView addSubview:mainView];

}

我想仅缩放所选索引,但其他子视图也会添加到屏幕而不进行缩放。

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [[[scrollView.subviews objectAtIndex:0] subviews] objectAtIndex:0] ;;

}

1 个答案:

答案 0 :(得分:0)

为了实现这一点,您必须采取主滚动视图,而不是在此滚动视图中显示imageView,您必须采取另一个滚动视图并显示图像,当用户尝试缩放图像时,只有选定的滚动视图滚动